Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/201.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Android Studio中使用后台服务发送通知?_Java_Android_Notifications - Fatal编程技术网

Java 如何在Android Studio中使用后台服务发送通知?

Java 如何在Android Studio中使用后台服务发送通知?,java,android,notifications,Java,Android,Notifications,我想在我的应用程序中发送推送通知,但几分钟后,android停止了我的后台服务,通知没有显示。如何制作android不会关闭的后台服务 BackroundService.class public Context context = this; public android.os.Handler handler = new Handler(); public static Runnable runnable = null; @Override public IBinder onBind(Int

我想在我的应用程序中发送推送通知,但几分钟后,android停止了我的后台服务,通知没有显示。如何制作android不会关闭的后台服务

BackroundService.class

public Context context = this;
public android.os.Handler handler  = new Handler();
public static Runnable runnable = null;

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    Log.e("Service", "Service crated!");

    runnable = new Runnable() {
        public void run() {
            Log.e("Service", "Service is still running!");
            Toast.makeText(context, "Service is still running", Toast.LENGTH_SHORT).show();
            handler.postDelayed(runnable, 50000);
        }
    };

    handler.postDelayed(runnable, 15000);
}

@Override
public void onDestroy() {

}

@Override
public void onStart(Intent intent, int startid) {
    Log.e("Service", "Service started by user!");
}
AlarmReceiver.class

@Override
public void onReceive(Context context, Intent intent) {

    int notificationId = intent.getIntExtra("ID", 0);
    String message = intent.getStringExtra("TEXT");
    String tittle = intent.getStringExtra("TITTLE");

    Intent mainIntent = new Intent(context, BackgroundService.class);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, mainIntent, 0);

    NotificationManager notificationManager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification.Builder builder = new Notification.Builder(context);
    builder.setSmallIcon(R.drawable.finance43)
            .setContentTitle(tittle)
            .setContentText(message)
            .setWhen(System.currentTimeMillis())
            .setAutoCancel(true)
            .setContentIntent(contentIntent)
            .setPriority(Notification.PRIORITY_HIGH)
            .setCategory(Notification.CATEGORY_MESSAGE);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        String channelId = "REMINDERS";
        NotificationChannel channel = new NotificationChannel(channelId,
                "Reminder",
                NotificationManager.IMPORTANCE_DEFAULT);
        notificationManager.createNotificationChannel(channel);
        builder.setChannelId(channelId);
    }
    notificationManager.notify(notificationId, builder.build());
}

当我启动应用程序时,我会看到“服务仍在运行!”大约1小时。

如果需要释放资源,系统将关闭该服务。但是,您可以通过将其作为前台服务启动,或者在可能的情况下指示重新启动来缓解此行为。更可能有一些有用的信息(特别是第二个答案)与前景服务,它的工作。谢谢如果需要释放资源,系统将终止服务。但是,您可以通过将其作为前台服务启动,或者在可能的情况下指示重新启动来缓解此行为。更可能有一些有用的信息(特别是第二个答案)与前景服务,它的工作。谢谢