all i this is my code in notification class which i am calling after completing my tasks from 1st activity.
But i am getting the problem for getting notification on current application.
I want to show Notification as dialog box.
"R.layout.main"
contains dialog box with OK button.
public class Notif extends Activity implements View.OnClickListener {
private Button Button01;
private NotificationManager mManager;
private static final int APP_ID = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.Button01 = (Button) this.findViewById( R.id.Button1);
this.Button01.setOnClickListener(this);
mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}
#Override
public void onClick(View v) {
Intent intent = new Intent(this,Notif.class);
Notification notification = new Notification(R.drawable.icon,
"Notify", System.currentTimeMillis());
notification.setLatestEventInfo(Notif.this,"App Name","Description of the notification",PendingIntent.getActivity(this.getBaseContext(), 0, intent,
PendingIntent.FLAG_CANCEL_CURRENT));
mManager.notify(APP_ID, notification);
}
}
1) Why handle your button listener with an implementation of View.OnClickListener?
The standard way I have seen so far is:
Button01.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Your code
}
});
2) Notifications are ways to notify the user through the Android status panel at the top of the screen. I don't understand what you want to do with Notifications and Dialog Boxes - make up your mind which one you want?
http://developer.android.com/guide/topics/ui/dialogs.html
http://developer.android.com/guide/topics/ui/notifiers/notifications.html
If you do want to use Notifications, then this is what I have in my onStop() method (it's basically just what you get from following the Android guide):
Notification notification = new Notification(R.drawable.icon, "App Name", System.currentTimeMillis());
notification.flags = Notification.FLAG_AUTO_CANCEL;
Intent notificationIntent = new Intent(this, ClassToStart.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(getApplicationContext(), "App Name", "Press here to resume", contentIntent);
mNotificationManager.notify(1, notification);
This is with mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); already done in onCreate()
Really not sure what it is you're trying to do.
Related
in my application, when i click list view item one notification will display, Now when i click that notification open another activity and display the notification content in another activity. Here when i select list view item notification will come with selected item text. After i select another list view item then also notification come with related text, but exact problem is when i open that notification, only one text will display in another activity.please fix it.
rmdListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
showNotification();
}
Now the notification method is
public void showNotification() {
Uri soundUri = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
// intent triggered, you can add other intent for other actions
Intent intent = new Intent(updateActivity.this,
NotificationReceiver.class);
intent.putExtra("Name", noti);
PendingIntent pIntent = PendingIntent.getActivity(updateActivity.this,
0, intent, 0);
Notification mNotification = new Notification.Builder(this)
.setContentTitle("Reminder!").setContentText(timeList + ":" + noti)
.setSmallIcon(R.drawable.logosmall).setContentIntent(pIntent)
.setSound(soundUri)
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, mNotification);
}
Finally i found answer for this task.First write this line into
onNewIntent(getIntent()); Oncreate() method.
After add this code to your Activity.
protected void onNewIntent(Intent intent) {
// TODO Auto-generated method stub
Bundle extras = getIntent().getExtras();
if(extras != null){
if(extras.containsKey("Name")){
setContentView(R.layout.notification);
// extract the extra-data in the Notification
ok_btn=(Button)findViewById(R.id.not_ok);
String msg = extras.getString("Name");
not_data = (TextView) findViewById(R.id.noti_txt);
not_data.setText(msg);
}
}
super.onNewIntent(intent);
}
any doubts in this task plse post ur question.
hi i have a problem with my code in the multiple alarm . i typed my code to be work on a multiple alarms every alarm take 10 seconds to be triggered REPEATEDLY. my problem that the alarm work with just one value but although i have put a different requestCode, but it cannot bet work or i don't know how i call every one separately .
the problem is i'v try to do this--->
alarm1 with a value and a request code 0 (for example) for 10 seconds
alarm2 with a different value and a request code 1 (for example) for 10 seconds
the code do this---->
desplay an just alarm2 that i selected lately
static int HELLO_ID = 1;
boolean flag = false;
int CountMultipleAlarm = 0;
EditText edt,edt2;
Button btn;
CountDownTimer timer;
//the strings of the notifications
String titlePills = "Time to take Panadol",DescriptionPills = "Panadol";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.planets_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
btn = (Button)findViewById(R.id.button1);
edt = (EditText)findViewById(R.id.editText1);
//hide the button and the edit text
btn.setVisibility(View.GONE);
edt.setVisibility(View.GONE);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final String edittext= edt.getText().toString();
Pattern pat= Pattern.compile("[0-9]");
// Pattern pat= Pattern.compile("[a-zA-Z0-9]");
Matcher matcher = pat.matcher(edittext);
//*************Timer Start *************************8
//11000 = 10 seconds(11000*6*60 == 1hour)
int count = 11000;
timer = new CountDownTimer(count, 1000)
{public void onTick(long millisUntilFinished)
{
long scnds=0;
scnds=(millisUntilFinished/1000);
}
public void onFinish()
{//Alaram cooooode **********************************************************
Calendar cal = Calendar.getInstance(); //for using this you need to import java.util.Calendar;
AlarmManager am = (AlarmManager)parent.getContext().getSystemService(Context.ALARM_SERVICE);
ArrayList<PendingIntent> intentArray = new ArrayList<PendingIntent>();
//the title and the description of the notification
Intent alarmintent = new Intent(parent.getContext(), Alarm_Receiver.class);
alarmintent.putExtra("title",titlePills + "value");
alarmintent.putExtra("note","value");
//HELLO_ID is a static variable that must be initialized at the BEGINNING OF CLASS with 1;
//example:protected static int HELLO_ID =1;
PendingIntent sender = PendingIntent.getBroadcast(parent.getContext(), CountMultipleAlarm++,
alarmintent,PendingIntent.FLAG_UPDATE_CURRENT);
//VERY IMPORTANT TO SET FLAG_UPDATE_CURRENT... this will send correct extra's informations to
//AlarmReceiver Class
// Get the AlarmManager service
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
//Alarm coooooode end **************************************
intentArray.add(sender);
timer.start();
}
}.start();
//*************Timer Ends *************************8
}
});
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}import
and this is the alarm_Receiver class
public class Alarm_Receiver extends BroadcastReceiver {
String notification1 = "You Pills Time ";
private static int NOTIFICATION_ID = 1;
#Override
public void onReceive(Context context, Intent intent) {
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationManager manger = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher, notification1,
System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(context,
NOTIFICATION_ID,
new Intent(context, Alarm_Receiver.class), 0);
Bundle extras=intent.getExtras();
String title=extras.getString("title");
//here we get the title and description of our Notification
//
String note=extras.getString("note");
notification.setLatestEventInfo(context, note, title, contentIntent);
notification.flags = Notification.FLAG_INSISTENT;
notification.defaults |= Notification.DEFAULT_SOUND;
//here we set the default sound for our
//notification
// The PendingIntent to launch our activity if the user selects this notification
manger.notify(NOTIFICATION_ID++, notification);
}
};
You are overwriting the old one because the request code that you use in getBroadcast() is always zero.
PendingIntent sender = PendingIntent.getBroadcast(parent.getContext(), i++, alarmintent,
PendingIntent.FLAG_UPDATE_CURRENT | Intent.FILL_IN_DATA);
The value of i is always zero, because you always set it to zero before calling getBroadcast().
Hi, I have a problem with my code.I typed my code to be work on a multiple alarms every alarm take 10 seconds to be triggered REPEATEDLY.My problem is that the alarm works with just one value but although I've put a different requestCode,but it doesn't work or I don't know how I can call every one separately .
I've tried to do this:
alarm1 with a value and a request code 0 (for example) for 10 seconds
alarm2 with a different value and a request code 1 (for example) for
10 seconds
The code does:
Display Alarm2 that I selected last.
static int HELLO_ID = 1;
boolean flag = false;
int CountMultipleAlarm = 0;
EditText edt,edt2;
Button btn;
CountDownTimer timer;
//the strings of the notifications
String titlePills = "Time to take Panadol",DescriptionPills = "Panadol";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.planets_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
btn = (Button)findViewById(R.id.button1);
edt = (EditText)findViewById(R.id.editText1);
//hide the button and the edit text
btn.setVisibility(View.GONE);
edt.setVisibility(View.GONE);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final String edittext= edt.getText().toString();
Pattern pat= Pattern.compile("[0-9]");
// Pattern pat= Pattern.compile("[a-zA-Z0-9]");
Matcher matcher = pat.matcher(edittext);
//*************Timer Start ******************
//11000 = 10 seconds(11000*6*60 == 1hour)
int count = 11000;
timer = new CountDownTimer(count, 1000)
{public void onTick(long millisUntilFinished)
{
long scnds=0;
scnds=(millisUntilFinished/1000);
}
public void onFinish()
//****** Alarm Code ********
Calendar cal = Calendar.getInstance();
// for using this you need to import java.util.Calendar;
AlarmManager am = (AlarmManager)parent.getContext().getSystemService(Context.ALARM_SERVICE);
ArrayList<PendingIntent> intentArray = new ArrayList<PendingIntent>();
//the title and the description of the notification
Intent alarmintent = new Intent(parent.getContext(), Alarm_Receiver.class);
alarmintent.putExtra("title",titlePills + "value");
alarmintent.putExtra("note","value");
//HELLO_ID is a static variable that must be initialized at the BEGINNING OF CLASS with 1;
//example:protected static int HELLO_ID =1;
PendingIntent sender = PendingIntent.getBroadcast(parent.getContext(), CountMultipleAlarm++,
alarmintent,PendingIntent.FLAG_UPDATE_CURRENT);
//VERY IMPORTANT TO SET FLAG_UPDATE_CURRENT... this will send correct extra's informations to
//AlarmReceiver Class
// Get the AlarmManager service
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
//Alarm coooooode end **************************************
intentArray.add(sender);
timer.start();
}
}
//************* Timer Ends *************
}
});
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}import
//Edit from Paramone: What comes after " }import " ???
alarm_Receiver class
public class Alarm_Receiver extends BroadcastReceiver {
String notification1 = "You Pills Time ";
private static int NOTIFICATION_ID = 1;
#Override
public void onReceive(Context context, Intent intent) {
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationManager manger = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher, notification1,
System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(context,
NOTIFICATION_ID,
new Intent(context, Alarm_Receiver.class), 0);
Bundle extras=intent.getExtras();
String title=extras.getString("title");
//here we get the title and description of our Notification
String note=extras.getString("note");
notification.setLatestEventInfo(context, note, title, contentIntent);
notification.flags = Notification.FLAG_INSISTENT;
notification.defaults |= Notification.DEFAULT_SOUND;
//here we set the default sound for our notification
// The PendingIntent to launch our activity if the user selects this notification
manger.notify(NOTIFICATION_ID++, notification);
}
};
suppose I click start button and close the application but Notification is running. After five miutes I open notification then second activity opens and settext with 5 min.Again i close appliction.Then again I click on start button.suppose after 3 min i check notification it should open with 3 min not 8.but in my application it show 8 min
1:Main ACtivity where notification is start
public class MainActivity extends Activity {
final Context context=this;
private EditText timerValue;
private static final int NOTIFY_ME_ID=1337;
Intent MyIntent;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
timerValue = (EditText) findViewById(R.id.timerValue111);
startButton = (Button) findViewById(R.id.startButton);
x=timerValue.getText().toString();
nman=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
startButton.setOnClickListener(new View.OnClickListener() {
#TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public void onClick(View view) {
Long s1=(long) 0.0;
String timeStamp = new SimpleDateFormat("HH:mm:ss").format(Calendar.getInstance().getTime());
//Toast.makeText(MainActivity.this,timeStamp,2000).show();
Date interestingDate = new Date();
s1= interestingDate.getTime();
int seconds = (int) (s1/ 1000) % 60 ;
int minutes = (int) ((s1 /(1000*60)) % 60);
int hours = (int) ((s1 /(1000*60*60)) % 24);
Toast.makeText(MainActivity.this,"Time:"+hours+":"+minutes+":"+seconds,2000).show();
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String MyText = "Reminder";
Notification mNotification = new Notification(R.drawable.ic_launcher, MyText, System.currentTimeMillis() );
//The three parameters are: 1. an icon, 2. a title, 3. time when the notification appears
String MyNotificationTitle = "Medicine!";
String MyNotificationText = "Don't forget to take your medicine!";
MyIntent = new Intent(MainActivity.this,SecondActivity.class);
MyIntent.putExtra("s1", (long)s1);
PendingIntent StartIntent = PendingIntent.getActivity(getApplicationContext(),0,MyIntent,0);
// x=timerValue.getText().toString();
//A PendingIntent will be fired when the notification is clicked. The FLAG_CANCEL_CURRENT flag cancels the pendingintent
mNotification.setLatestEventInfo(getApplicationContext(), MyNotificationTitle, MyNotificationText, StartIntent);
mNotification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, mNotification);
}
});
}
}
2:Second Activity where total time is calculated
public class SecondActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
long p=(long) 0.0;
long s=(long) 0.0;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
TextView text1=(TextView)findViewById(R.id.text1);
Bundle extras = getIntent().getExtras();
Date interestingDate = new Date();
s= interestingDate.getTime();
long x1=extras.getLong("s1");
p=s-x1;
int minutes = (int) ((p /(1000*60)) % 60);
text1.setText(""+minutes);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.second, menu);
return true;
}
}
That is happening maybe because you do not close the application, so the application goes in onPause state, and your second activity is not calling the onDestroy => when you enter again in the second activty, is called the onResume method, where you do not set the TextView text
I have created an activity in which I have created a progress bar as follows, and I have written the Notification but it's not working. Notification occurs at top corner of the screen sometimes. I want that Notification as a message box in the middle of the screen. How to achieve it? My activity code as follows:
public class oddg extends Activity implements OnClickListener
{
ProgressDialog dialog;
int increment;
int maximum ;
private NotificationManager mManager;
private static final int APP_ID = 0;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
Button startbtn = (Button) findViewById(R.id.startbtn);
startbtn.setOnClickListener(this);
mManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
}
#Override
public void onClick(View arg0)
{
// get the increment value from the text box
EditText et = (EditText) findViewById(R.id.increment);
// convert the text value to a integer
increment = Integer.parseInt(et.getText().toString());
dialog = new ProgressDialog(this);
dialog.setCancelable(true);
dialog.setMessage("Loading...");
// set the progress to be horizontal
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
// reset the bar to the default value of 0
dialog.setProgress(0);
// get the maximum value
EditText max = (EditText) findViewById(R.id.maximum);
// convert the text value to a integer
maximum = Integer.parseInt(max.getText().toString());
// set the maximum value
dialog.setMax(maximum);
// display the progressbar
dialog.show();
// create a thread for updating the progress bar
Thread background = new Thread (new Runnable() {
public void run()
{
try
{
// enter the code to be run while displaying the progressbar.
// This example is just going to increment the progress bar:
// So keep running until the progress value reaches maximum value
while(dialog.getProgress()<= dialog.getMax())
{
// wait 500ms between each update
Thread.sleep(500);
// active the update handler
progressHandler.sendMessage(progressHandler.obtainMessage());
}
}
catch (java.lang.InterruptedException e)
{
// if something fails do something smart
}
}
});
// start the background thread
background.start();
}
// handler for the background updating
Handler progressHandler = new Handler()
{
public void handleMessage(Message msg)
{
if(dialog.getProgress()== dialog.getMax())
{
Notification notification = new Notification(R.drawable.icon,"Click here", System.currentTimeMillis());
notification.setLatestEventInfo(oddg.this,"App Name","Description of the notification",PendingIntent.getActivity(getApplicationContext(), CONTEXT_IGNORE_SECURITY, getIntent(), CONTEXT_IGNORE_SECURITY));
}
dialog.incrementProgressBy(increment);
}
};
It would be something like this
notificationMgr = context.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0,
new Intent(ctx, myActClassName.class), 0);
Notification notification = new Notification(R.drawable.icon, someText, System.currentTimeMillis());
notification.setLatestEventInfo(ctx, title, contentText, contentIntent);
int HELLO_ID = 10;
notificationManager.notify(HELLO_ID, notification);
For Status bar Notification check this
http://developer.android.com/guide/topics/ui/notifiers/notifications.html
http://javainnovations.blogspot.com/2009/05/how-to-send-message-to-status-bar-in.html