Android 如何在通知图标单击时打开应用程序?
我知道可能是重复的,但我正在使用AlarmManager在几个间隔后获取通知 我使用以下代码来显示通知Android 如何在通知图标单击时打开应用程序?,android,performance,android-intent,notifications,android-webview,Android,Performance,Android Intent,Notifications,Android Webview,我知道可能是重复的,但我正在使用AlarmManager在几个间隔后获取通知 我使用以下代码来显示通知 public class MainActivity extends Activity { public void setRepeatingAlarm() { Intent intent = new Intent(this, TimeAlarm.class); PendingIntent pendingIntent = PendingIntent.get
public class MainActivity extends Activity {
public void setRepeatingAlarm() {
Intent intent = new Intent(this, TimeAlarm.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
intent, PendingIntent.FLAG_CANCEL_CURRENT);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
(50 * 1000), pendingIntent);
}
}
public class TimeAlarm extends BroadcastReceiver {
NotificationManager nm;
@Override
public void onReceive(Context context, Intent intent) {
nm = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
new Intent(), 0);
Notification noti = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setTicker("ticker")
.setWhen(System.currentTimeMillis()+50000)
.setContentTitle("title")
.setContentText("Check out ")
.setContentIntent(contentIntent)
//At most three action buttons can be added
.setAutoCancel(true).build();
int notifyID =1;
nm.notify(notifyID, noti);
}
}
一切都是好的,但是
单击该通知图标时未打开我的应用程序`
看看我用过的代码,它非常适合我
Long rowId = intent.getExtras().getLong(TaskDatabase.KEY_ROWID);
String a = intent.getExtras().getString(TaskDatabase.KEY_TITLE);
int count = 0;
int i = 1;
String body = "Task needs your attention.";
String title = "Reminder";
NotificationManager mgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(this, Main.class); // Change it any activity, you want it to open.
notificationIntent.putExtra(TaskDatabase.KEY_ROWID, rowId);
PendingIntent pi = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification note = new Notification(R.drawable.white, body, System.currentTimeMillis());
note.setLatestEventInfo(this, a, body, pi);
SharedPreferences preference = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
String ringPreference = preference.getString("Ringtone", "DEFAULT_SOUND");
note.defaults |= Notification.DEFAULT_SOUND;
note.flags |= Notification.FLAG_AUTO_CANCEL;
note.sound = Uri.parse(ringPreference);
int id = (int)((long)rowId);
mgr.notify(id, note);
最后我让它工作起来了非常感谢大家
@Override
public void onReceive(Context context, Intent intent) {
Intent notificationIntent = new Intent(context, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification noti = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setTicker("ticker")
//.setLargeIcon(largeIcon)
//.setWhen(System.currentTimeMillis()+1000)
.setContentTitle("title")
.setContentText("message!")
.setContentIntent(contentIntent)
//At most three action buttons can be added
.setAutoCancel(true).build();
int notifyID =0;
notificationManager.notify(notifyID, noti);
}
Notification note=新通知已弃用朋友:(请修改我的代码片段我是android新手,这将非常有帮助,谢谢