Reputation: 1105
I want to give option of deleting my notification in the activity which gets opened after clicking it from notification bar. I am using the following code:
nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Log.i(TAG, "after NotificationManager");
Intent intent = new Intent(this,test.class);
intent.putExtra("title", title);
intent.putExtra("subject", subject);
intent.putExtra("notificationid", count);
Log.i(TAG, title);
Log.i(TAG, subject);
PendingIntent pi = PendingIntent.getActivity(this, count, intent,0);
Notification n = new Notification(R.drawable.icon,subject,System.currentTimeMillis());
n.setLatestEventInfo(this, title, subject, pi);
n.defaults = Notification.DEFAULT_LIGHTS;
nm.notify(count, n);
And my test.java looks like this:
public class test extends Activity {
TextView Title;
TextView Subject;
Button Clear;
protected int notificationid;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
String title="";
String subject="";
Bundle extras = getIntent().getExtras();
if(extras !=null) {
title = extras.getString("title");
subject = extras.getString("subject");
notificationid = extras.getInt("notificationid");
}
Title = (TextView) findViewById(R.id.Title);
Subject = (TextView) findViewById(R.id.Subject);
Title.setText(title);
Subject.setText(subject);
Clear.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
NotificationManager nm ;
nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Log.i(getClass().getSimpleName(), " onClick inside test caled....");
nm.cancel(notificationid);
}
});
}
}
But application is getting closed unexpectedly. Can anyone help me out in this? Thanks!!
Upvotes: 1
Views: 936
Reputation: 171
Try to use FLAG_AUTO_CANCEL:
n.flags |= Notification.FLAG_AUTO_CANCEL;
Bit to be bitwise-ored into the flags field that should be set if the notification should be canceled when it is clicked by the user.
Upvotes: 0
Reputation: 29968
You are missing
Clear=(Button) findViewById(R.id.ClearButtonId);
this in your test
Class ?
It must be the problem. You might be getting NullPointerException
as you have not parsed the Button
component and you are trying to use it directly.
Upvotes: 2
Reputation: 4958
Without being able to see your LogCat output, it's hard to tell what exactly the error is, but from looking at the code (this may of course be wrong if you've only pasted partial code), you're not initialising your notificationid
variable in the case where extras == null
. If you're developing with Eclipse, have a look in the LogCat view and find the error stacktrace.
Edit after seeing Kyle's answer: what he said about LogCat ;-)
Upvotes: 1
Reputation: 39480
What is the error associated with the unexpected code? Can you give details on the exception causing the crash?
Do you use LogCat? If not, check out http://developer.android.com/guide/developing/tools/logcat.html. To view the logcat logs directly in the command prompt, open the command prompt and go to the directory where your android sdk is found. Run adb logcat
. You should start seeing a printout of the errors, warnings, etc. generated by logcat.
Once you're able to see what the problem is using logcat (it will tell you NullPointerException, ResourceNotFoundException, etc and the line of code that caused the problem), if you can't figure out the source of the crash from that then paste the error info here.
Upvotes: 1