主屏幕上通知的安卓持续时间



我想在主屏幕上显示通知。这是我的代码:

Notification.Builder mBuilder = new Notification.Builder(getApplicationContext())
.setContentTitle("Connection request")
.setContentText("content text")
.setSmallIcon(R.drawable.ic_done)
.setContentIntent(pi)
.setPriority(Notification.PRIORITY_HIGH);
if (Build.VERSION.SDK_INT >= 21)
   mBuilder.setVibrate(new long [0]);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(getApplicationContext().NOTIFICATION_SERVICE);
mNotificationManager.notify(1300, mBuilder.build());

此步骤中一切正常,但通知在 5 秒后消失。

是否可以像我们收到来电时那样创建通知?还是其他持续时间?我的意思是在主屏幕上。

我知道这是可能的,因为它适用于WhatsApp等应用程序。

我尝试设置长振动和长声音,

但通知在 5 秒后从主屏幕上消失,声音和振动继续播放。

谢谢!

编辑

public class WaitConnectionService extends Service {
public final static int START_CONNECTION = 1;
private NotificationManager mNotificationManager;
private Looper mServiceLooper;
private ServiceHandler mServiceHandler;
private final class ServiceHandler extends Handler {
    public ServiceHandler(Looper looper) {
        super(looper);
    }
    @Override
    public void handleMessage(final Message msg) {
        System.out.println("HANDLE MESSAGE");
        if (msg.what == START_CONNECTION) {
            new Thread(new Runnable() {
                @Override
                public void run() {
//                        new TCPServer().run();
                    Intent i = new Intent(WaitConnectionService.this, ListActivity.class);
                    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                            Intent.FLAG_ACTIVITY_SINGLE_TOP);
                    PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0,
                            i, 0);
                    Intent intent = new Intent(getApplicationContext(), SettingsActivity.class);
                    PendingIntent pIntent = PendingIntent.getActivity(getApplicationContext(),
                            (int) System.currentTimeMillis(), intent, 0);
                    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext())
                            .setContentTitle("Connection request")
                            .setContentText("content text")
                            .setSmallIcon(R.drawable.ic_done)
                            .setContentIntent(pi)
                            .setPriority(Notification.PRIORITY_MAX)
                            .setWhen(System.currentTimeMillis())
                            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                            .setUsesChronometer(true)
                            .addAction(new NotificationCompat.Action.Builder(R.drawable.ic_done,
                                    "ok", pIntent).build());
                    if (Build.VERSION.SDK_INT >= 21)
                        mBuilder.setVibrate(new long[0]);
                    NotificationManager mNotificationManager =
                            (NotificationManager) getSystemService(getApplicationContext().NOTIFICATION_SERVICE);
                    mNotificationManager.notify(1300, mBuilder.build());
                }
            }).start();
        }
    }
}
@Override
public void onCreate() {
    System.out.println("ON CREATE");
    mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    HandlerThread thread = new HandlerThread("My handler thread", Process.THREAD_PRIORITY_BACKGROUND);
    thread.start();
    mServiceLooper = thread.getLooper();
    mServiceHandler = new ServiceHandler(mServiceLooper);
}
@Override
public int onStartCommand(Intent intent, int flags, final int startId) {
    System.out.println("ON START COMMAND");
    Message msg = mServiceHandler.obtainMessage();
    msg.what = START_CONNECTION;
    msg.arg1 = startId;
    mServiceHandler.sendMessage(msg);
    return START_NOT_STICKY;
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}
@Override
public void onDestroy() {
    System.out.println("Service onDestroy(");
}
}

在活动中:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...
    Intent intent1 = new Intent(this, WaitConnectionService.class);
    intent1.putExtra("what", 1);
    startService(intent1);
}

当我运行此代码时,它会启动主要活动,并且仅显示 5 秒的通知。

从此示例中查找解决方案

https://github.com/googlesamples/android-LNotifications

您将返回START_NOT_STICKY onStartCommand()服务。这意味着您的服务将自动停止。而是使用 START_STICKY .

最新更新