Android主屏幕上的通知持续时间

时间:2016-02-15 10:53:38

标签: android notifications

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

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

1 个答案:

答案 0 :(得分:0)

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