【问题标题】:How can I put notification icon in notification area while app is running android?应用程序运行android时,如何将通知图标放在通知区域?
【发布时间】: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);

它会像我想要的那样工作,但仅在模拟器上,当我在真实设备中运行此代码时,它会崩溃。

如何在我的应用程序中完成这项任务?如果没有IntentPendingIntent,如何放置通知图标?

【问题讨论】:

    标签: android notifications statusbar


    【解决方案1】:

    您可以使用此代码,如答案herehere 中所述:

    PendingIntent contentIntent = PendingIntent.getActivity(
        getApplicationContext(),
        0,
        new Intent(), // add this
        PendingIntent.FLAG_UPDATE_CURRENT);
    

    【讨论】:

      【解决方案2】:

      这与通知或通知管理器无关 您可以通过将您的一行代码更改为此来做到这一点,

      PendingIntent pIntent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT);
      

      【讨论】:

        猜你喜欢
        • 2012-09-27
        • 1970-01-01
        • 2017-12-02
        • 1970-01-01
        • 2015-05-14
        • 2019-10-18
        • 2022-12-10
        • 2010-11-18
        • 1970-01-01
        相关资源
        最近更新 更多