【发布时间】:2015-06-18 22:10:22
【问题描述】:
我想在我的应用运行时将我的应用图标放在通知区域。我进行了很多搜索并使其成为可能,以将其显示在通知区域中。
public class MainActivity extends ActionBarActivity {
NotificationManager mNotificationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Context context = getApplicationContext();
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setTicker("This is a new notification")
.setContentTitle("Notification")
.setContentText("App runing..")
.setSmallIcon(R.mipmap.ic_launcher);
Intent intent = new Intent(context, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);
builder.setContentIntent(pIntent);
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notif = builder.build();
mNotificationManager.notify(1, notif);
}
@Override
protected void onDestroy() {
super.onDestroy();
mNotificationManager.cancel(1);
}
}
但我想要的是我不想在点击通知时打开任何活动。
例如:电池充满时出现的电池信息图标,它不会打开任何应用程序或活动,也不会将其从通知区域中删除。
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);
通过将这一行代码更改为此
PendingIntent pIntent = PendingIntent.getActivity(context, 0, null, 0);
它会像我想要的那样工作,但仅在模拟器上,当我在真实设备中运行此代码时,它会崩溃。
如何在我的应用程序中完成这项任务?如果没有Intent 和PendingIntent,如何放置通知图标?
【问题讨论】:
标签: android notifications statusbar