簡體   English   中英

Android-如何將正在進行的事件放在狀態欄上而不在通知屏幕上?

[英]Android - How to put an ongoing event on status bar but not on notification screen?

在我的應用程序中,我添加了正在進行的通知。 該通知在狀態欄上有一個圖標,並且在通知區域中有更多詳細信息。

此圖片來自Google。 這是通知區域:

此圖片來自Google。這是通知區域

我需要狀態欄上的圖標始終保持在該位置,但是用戶將能夠在通知屏幕中關閉該通知,因此用戶只能使用狀態欄上的圖標。 我該怎么做?

我的服務代碼:

   @SuppressWarnings("static-access")
   @Override
   public int onStartCommand(Intent intent, int flag, int startId)
   {
        super.onStartCommand(intent, START_STICKY, startId);

        ...

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
            .setContentTitle(getString(R.string.myNotificationString))
            .setContentText(string1 + "  |  " + string2)
            .setLargeIcon(appIcon)
            .setSmallIcon(getResources().getIdentifier("icon_" + getCurrentIconNumber(),"drawable", getPackageName()));  // the icon changes every day

        Intent resultIntent = new Intent(this, MainActivity.class);
        PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0 ,resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(resultPendingIntent);
        Notification notification = mBuilder.build();
        notification.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;

        mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        mNotifyMgr.notify(myRequestCode, notification);

        return START_STICKY;
    }

嘗試如下定義resultPendingIntent

PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0 ,resultIntent, PendingIntent.FLAG_ONGOING_EVENT);

編輯1

   @SuppressWarnings("static-access")
   @Override
   public int onStartCommand(Intent intent, int flag, int startId)
   {
        super.onStartCommand(intent, START_STICKY, startId);

        ...

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
            .setContentTitle(getString(R.string.myNotificationString))
            .setContentText(string1 + "  |  " + string2)
            .setLargeIcon(appIcon)
            .setSmallIcon(getResources().getIdentifier("icon_" + getCurrentIconNumber(),"drawable", getPackageName()));  // the icon changes every day

        Intent resultIntent = new Intent(this, MainActivity.class);
        PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0 ,resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(resultPendingIntent);
        Notification notification = mBuilder.build();
        notification.flags = Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;

        mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        mNotifyMgr.notify(myRequestCode, notification);

        return START_STICKY;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM