Hello,
I would like to make an app that flashes the led from my phone (nexus 5) in different colors .
Code:
package com.example.led1;
//import java.util.Timer;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.view.Menu;
//import android.graphics.Color;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
Notification notf = new NotificationCompat.Builder(this)
.setAutoCancel(false)
.setLights(0xffff00, 1000, 100)
.setLights(0xff0000, 5000, 100)
.setLights(0x0000FF, 10000, 100)
.build();
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(2, notf);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
When i run this code only the last .setLights command works and thus only one color is used by the led. How can i make the led turn to all the three colors in the writen order ?
You will have to make three different light calls that have a pause in the middle. So to say you can do that by using delayed task for example.
But you will have to look tasks up in the android dev docs, cause i dont know how to implement that at the moment, sry
---------------------------------
Phone : My new Nexus 4!
OS:
Pure KitKat 4.4.2 stock, no root, no mods (but only for the first time ;D)
---------------------------------
Gesendet von Tapatalk
---------- Post added at 03:56 PM ---------- Previous post was at 03:55 PM ----------
Or do you want to make the led shine in three different colors at the same time??
Thats not possible, im afraid
---------------------------------
Phone : My new Nexus 4!
OS:
Pure KitKat 4.4.2 stock, no root, no mods (but only for the first time ;D)
---------------------------------
Gesendet von Tapatalk
Ya as said above you need to make three different notifications and realy them at desired intervals you can use the Update notification fn. so you dont post a new notification each time
Try using TimerTasks / Handler
Sent from my GT-S5302 using Tapatalk 2
Related
Hey all,
Finally I have started learning android app and now I need to make a Preference screen.I have made an xml called info.xml in res/layout and have also added codes to it.Now I created a new class called Info and the code contains:
Code:
package com.test.info;
import android.os.Bundle;
//import android.app.Activity;
import android.view.Menu;
import android.preference.PreferenceActivity;
public class Info extends PreferenceActivity {
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.info);
addPreferencesFromResource(R.layout.info);
}
[user=439709]@override[/user]
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.info, menu);
return true;
}
}
But I get
Code:
The method addPreferencesFromResource(int) from the type PreferenceActivity is deprecated
I know the solution is pretty easy but I cannot find the solution .Hope anybody helps me soon...
My app has min sdk 4.0.3,max sdk 4.2 and target sdk 4.2.Thanks.
Regards,
Vijai
EDIT:As I said,Its indeed easy fix and I solved it .Requested to lock the thread
Closed on OP's request
hello I need help i need it to when the user clicks a button in my app is calls a specific phone number when I try doing it on my phone i click the button and it says app has crashed
Please help heres my class file:
Code:
package com.d4a.kingdomapp;
import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.net.Uri;
public class ComputerIssues extends Activity {
String number = "573-642-2800";
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.computerissues);
// Here is code to go grab and layout the Buttons, they're named b1, b2, etc. and identified as such.
Button callfulton =(Button)findViewById(R.id.fultonoffice);
callfulton.setOnClickListener(buttonhandler);
}
View.OnClickListener buttonhandler=new View.OnClickListener() {
// Now I need to determine which button was clicked, and which intent or activity to launch.
public void onClick(View v) {
switch(v.getId()) {
// Now, which button did they press, and take me to that class/activity
case R.id.fultonoffice: //<<---- notice end line with colon, not a semicolon
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" +number));
startActivity(intent);
}
}
};
}
Thanks in advance
Regards
Rapsong11
Have you declared the permission in your menifest?
Add this permission
Yes I have
Thanks for the reply
Sent from my Nexus 4 using xda app-developers app
Hey guys, im on the process of building my app. Basically, so far, this is what should happen:
The user presses a button and a dialogbox appears. In the dialogbox, the user selects the days in which the reminder shall remind him. (If that makes sense...).
This is my Java code:
Code:
package yCM.medireminder;
import java.util.ArrayList;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class ADD extends Activity implements OnClickListener{
private AlertDialog.Builder dialogBuilder;
private void days()
{
//Declaring the Variables
final ArrayList weekdays = new ArrayList();
dialogBuilder = new AlertDialog.Builder(this);
final String[] dayss = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
//Starting the Process
dialogBuilder.setTitle("Check the Days");
dialogBuilder.setMessage("Check the days when you will shall be reminded");
dialogBuilder.setMultiChoiceItems(dayss, null, new DialogInterface.OnMultiChoiceClickListener() {
[user=439709]@override[/user]
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
weekdays.add(which);
}else if(weekdays.contains(which))
{
weekdays.remove(Integer.valueOf(which));
}
}
});
dialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
[user=439709]@override[/user]
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Your days have been set", Toast.LENGTH_LONG);
}
});
dialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
[user=439709]@override[/user]
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Your days have not been set", Toast.LENGTH_LONG);
}
});
// OUTPUT
AlertDialog dayDialog = dialogBuilder.create();
dayDialog.show();
}
Does everything look alright, the emulator isn't working for my and I have misplaced my phone (its on silent) so I have no way of checking whether it will work. I have linked the dialog box to a button so Im sure that when the button is clicked, the dialog box should appear.
It is recommended to use DialogFragments instead of simple dialogs. That way the system would orientation changes etc. for you.
However, you would need to convert your Activity into a FragmentActivity.
nikwen said:
It is recommended to use DialogFragments instead of simple dialogs. That way the system would orientation changes etc. for you.
However, you would need to convert your Activity into a FragmentActivity.
Click to expand...
Click to collapse
I do plan on making the app on permenantly on portrait mode so I don't think the orientation would be any problem even when the user switches to landscape.
Why would I need to convert my activity to fragmentactivity? What does this do? Also my min sdk is set to api level 8 (2.2 i believe).
Other than that, does the code seem alright?
TwilightLoz said:
I do plan on making the app on permenantly on portrait mode so I don't think the orientation would be any problem even when the user switches to landscape.
Why would I need to convert my activity to fragmentactivity? What does this do? Also my min sdk is set to api level 8 (2.2 i believe).
Other than that, does the code seem alright?
Click to expand...
Click to collapse
You just need to do this if you want to use the DialogFragment. For the normal dialog you don't need it.
You can use fragments on Android 1.6+ if you include the supportv4 library.
Try it. That's all I can say. There might be other things that make it crash, apart from that code. Maybe related to the Android lifecycle.
thanks for the info. Just runned it, everythings working smooth for now. Any idea on how to make the android system recognise/link the days of the week on my string? I did have a read through this but didn't quite understand it. I have a feeling that I should be using something got to do with the calender though...
http://developer.android.com/reference/java/util/Date.html#getDay()
TwilightLoz said:
thanks for the info. Just runned it, everythings working smooth for now. Any idea on how to make the android system recognise/link the days of the week on my string? I did have a read through this but didn't quite understand it. I have a feeling that I should be using something got to do with the calender though...
http://developer.android.com/reference/java/util/Date.html#getDay()
Click to expand...
Click to collapse
You can get the current time using
Code:
System.currentTimeMillis()
Then create a Date from it:
Code:
Date myDate = new Date(System.currentTimeMillis();
Then you can format it using the SimpleDateFormat class: http://developer.android.com/reference/java/text/SimpleDateFormat.html
For some things the Calendar class might be better: http://developer.android.com/reference/java/util/Calendar.html
However, I have not worked with it yet.
I have been trying to implement the broadcast receiver programatically , but my unregister method is not working or the app is working even if it in killed state .What i'm trying to accomplish is to display a test message ,when a sms receives ,,i have two buttons in app,register and unregister .If the user presses register ,no matter whether the app is running foreground or background the app should display the toast message,but if i press unregister the app should not invoke toast message .The code is
Code:
package gates.apps.automaticmessageresponder;
import android.app.Activity;
import android.content.ComponentName;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
SmsReceiver broadcastReceiver=new SmsReceiver();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void register(View view){
this.registerReceiver(broadcastReceiver, new IntentFilter(
"android.provider.Telephony.SMS_RECEIVED"));
Log.e("register","pressed");
}
public void unRegister(View view){
this.unregisterReceiver(broadcastReceiver);
Log.e("unregister","pressed");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
and another class is
Code:
package gates.apps.automaticmessageresponder;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;
public class SmsReceiver extends BroadcastReceiver {
// Get the object of SmsManager
final SmsManager sms = SmsManager.getDefault();
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
// Retrieves a map of extended data from the intent.
final Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage.getDisplayOriginatingAddress();
String senderNum = phoneNumber;
String message = currentMessage.getDisplayMessageBody();
Log.i("SmsReceiver", "senderNum: "+ senderNum + "; message: " + message);
// Show alert
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, "senderNum: "+ senderNum + ", message: " + message, duration);
toast.show();
} // end for loop
} // bundle is null
} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" +e);
}
}
}
i have not modified the manifest ,except adding sms recieve permission
But the problem with above code is ,even i don't press register it's getting invoked and even if i press unregister button ,it's not stopping
Dude you haven't added a listener for on click(unless you have specified register in layout for button). Nor can I see the button defined. Plus your toast message falls in try catch block. So look for clues in that. I think it'll work once you correct the code. I'll help more if its possible.
Please give a thanks if you think this post helped you!
Sent from my Nexus 4 using XDA Premium 4 Mobile App .
hey i have been learing java for a while and just have started coding for android last night
i am learing android development off the new boston youtube page
i have created some buttons in xml and assigned them ids and now would like to add an on click listener.
this is my code
Code:
package com.example.time;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.os.Build;
public class StartingPoint extends ActionBarActivity {
Button yes,no;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_starting_point);
yes = (Button) findViewById(R.id.bno);
no = (Button) findViewById(R.id.byes);
add.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
eclipse created an onclicklistner but has an error with add
the tutorial i am learning of of is from the late 2000s is there a new way or something have i done something wrong please help
this is my first post yaaah
sorry if this isnt allowed in this section of the forum i see that people are asking question here aswell but when i go to post this threads it says that you cant post it here
any way thxs for ur help in advance
beefmaster said:
hey i have been learing java for a while and just have started coding for android last night
i am learing android development off the new boston youtube page
i have created some buttons in xml and assigned them ids and now would like to add an on click listener.
this is my code
Code:
package com.example.time;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.os.Build;
public class StartingPoint extends ActionBarActivity {
Button yes,no;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_starting_point);
yes = (Button) findViewById(R.id.bno);
no = (Button) findViewById(R.id.byes);
add.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
eclipse created an onclicklistner but has an error with add
the tutorial i am learning of of is from the late 2000s is there a new way or something have i done something wrong please help
this is my first post yaaah
sorry if this isnt allowed in this section of the forum i see that people are asking question here aswell but when i go to post this threads it says that you cant post it here
any way thxs for ur help in advance
Click to expand...
Click to collapse
It is the right way to do it, only you messed up the names. If you have two Buttons in your layout (yes and no), you set an onClickListener on one of them calling <buttonName>.setOnClickListener(...);, so in this case yes.setOnClickListener(...). Also I would suggest renaming your Buttons in the code to bYes and bNo or something like that so you know it is a Button. And yes, it is the right section :good:
Dont you also have to declare the buttons with
Button yes = (Button)findViewById(R.id.byes);
---------------------------------
Phone : Nexus 4
OS:
Pure KitKat 4.4.2 stock, no root, no mods
---------------------------------
4d 61 73 72 65 70 75 73 20 66 74 77
Gesendet von Tapatalk
Masrepus said:
Dont you also have to declare the buttons with
Button yes = (Button)findViewById(R.id.byes);
Click to expand...
Click to collapse
If you do it this way, "yes" is only declared in the scope of the onCreate method. It is possible, but for views which sit in memory anyway it's more convenient to have the those delared as instance variables like he did above, right below the class declaration.
SimplicityApks said:
If you do it this way, "yes" is only declared in the scope of the onCreate method. It is possible, but for views which sit in memory anyway it's more convenient to have the those delared as instance variables like he did above, right below the class declaration.
Click to expand...
Click to collapse
oh didnt see that
---------------------------------
Phone : Nexus 4
OS:
Pure KitKat 4.4.2 stock, no root, no mods
---------------------------------
4d 61 73 72 65 70 75 73 20 66 74 77
Gesendet von Tapatalk
Dude I think u Have Interchanged the functions of Button yes and Button no.
yes = (Button)findViewByID(R.id.byes);
no = (Button)findViewByID(R.id.bno);
yes.setonClickListener(new View.OnClickListener() {
@override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
no.setonClickListener(new View.OnClickListener() {
@override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
Also Just For the sake of testing you can add a toast message as below
yes.setonClickListener(new View.OnClickListener() {
@override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast t = Toast.makeText(getApplicationContext(),"YES Pressed",Toast.LENGTH_SHORT);
t.show();
}
});
no.setonClickListener(new View.OnClickListener() {
@override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast t = Toast.makeText(getApplicationContext(),"NO Pressed",Toast.LENGTH_SHORT);
t.show();
}
});
Also Check the activity_starting_point.xml file if you have messed with the Button IDs.'
Hit Thanks If I have Helped you ......
Masrepus said:
Dont you also have to declare the buttons with
Button yes = (Button)findViewById(R.id.byes);
Click to expand...
Click to collapse
You don't have to cast to Button class in this particular case, View has already defined method "setOnClickListener".
And yes, you better declare field to keep reference and avoid additional findViewById() calls.