Android 在通知图标单击时启动新活动
我已经可以在通知栏中显示一个图标,如下面的代码所示。当用户单击此图标时,我想启动一个新活动(Android 在通知图标单击时启动新活动,android,Android,我已经可以在通知栏中显示一个图标,如下面的代码所示。当用户单击此图标时,我想启动一个新活动(myclass),但我不知道如何执行此操作。我应该把意图放在哪里 public class NotificationActivity extends Activity { AlarmManager am; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(saved
myclass
),但我不知道如何执行此操作。我应该把意图放在哪里
public class NotificationActivity extends Activity {
AlarmManager am;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification);
am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
setRepeatingAlarm();
}
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(),
(20 * 1000), pendingIntent);
System.out.println("Calling Alaram...");
}
}
public class BootUpReciever extends BroadcastReceiver {
NotificationManager nm;
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("in broad....");
nm = (NotificationManager) context .getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence from = "Lokesh";
CharSequence message = "Notification Test...";
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
new Intent(), 0);
Notification notif = new Notification(R.drawable.cherry_icon,
"Notification Test...", System.currentTimeMillis());
notif.setLatestEventInfo(context, from, message, contentIntent);
nm.notify(1, notif);
if ((intent.getAction() != null) &&
(intent.getAction().equals("android.intent.action.BOOT_COMPLETED")))
{
System.out.println("in broadcast receiver.....");
Intent i = new Intent(context, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
您提供给通知生成器的挂起意图应包含在用户单击通知时启动活动的意图。您需要创建一个启动活动的意图:
Intent resultIntent = new Intent(this, ResultActivity.class);
然后使用以下意图制作悬挂式帐篷:
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
然后使用它创建一个通知。当用户单击您的通知时,ResultActivity
将启动
整个代码片段:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!");
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, ResultActivity.class);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(ResultActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.build());
浏览开发人员文档中的详细文章,以获得一个很好的解释:
您给通知生成器的挂起意图应包含一个意图,该意图将在用户单击通知时启动活动。您需要创建一个启动活动的意图:
Intent resultIntent = new Intent(this, ResultActivity.class);
然后使用以下意图制作悬挂式帐篷:
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
然后使用它创建一个通知。当用户单击您的通知时,ResultActivity
将启动
整个代码片段:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!");
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, ResultActivity.class);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(ResultActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.build());
浏览开发人员文档中的详细文章,以获得一个很好的解释:
您是否在清单中声明了您的新活动@NiceGuyyes是个说辞,但当点击通知栏上的图标时,我将启动新活动的意图放在代码中的什么地方???检查这个问题:您是否在清单中声明了您的新活动@NiceGuyyes是一个说辞,但我在代码中放置了当点击通知栏上的图标时开始新活动的意图???检查这个问题:很好的解决方案,但这只适用于您有多个活动的情况,如果您只有一个活动,而这一个是singleInstance,仅当活动位于后台时才将其带到前台的通知屏幕保持空白。这是一个不错的解决方案,但这仅适用于您有多个活动的情况,如果您只有一个且此活动为singleInstance,则仅当活动位于后台时才将其带到前台的通知屏幕保持空白。