如果我使用 intent 进入主屏幕,Android 服务将被终止

标签 android service android-service

我有一个前台服务,它会在引导完成时启动。一切正常,除非我打开我的应用程序并按下退出按钮,服务将被终止。如果我通过任务管理器终止应用程序,则没有问题。

这是我在退出按钮上写的

                 Intent homeIntent = new Intent(Intent.ACTION_MAIN);
                 homeIntent.addCategory( Intent.CATEGORY_HOME );
                 homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                 startActivity(homeIntent);
                 finish();

开始我的服务

        Intent startIntent = new Intent(Splash.this, MyService.class);
        startIntent.setAction(Constants.ACTION.STARTFOREGROUND_ACTION);
        startService(startIntent);

//服务类

public void onCreate() {
    super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (intent.getAction().equals(Constants.ACTION.STARTFOREGROUND_ACTION)) {
        //Log.i(LOG_TAG, "Received Start Foreground Intent ");
        db=new DatabaseSqlite(this);
        showNotification();
        StartThread startThread =new StartThread();
        startThread.start();
        Toast.makeText(this, "Service Started!", Toast.LENGTH_SHORT).show();

    } else if (intent.getAction().equals(Constants.ACTION.PREV_ACTION)) {
        Log.i(LOG_TAG, "Clicked Previous");

        Toast.makeText(this, "Clicked Previous!", Toast.LENGTH_SHORT)
                .show();
    } else if (intent.getAction().equals(Constants.ACTION.PLAY_ACTION)) {
        Log.i(LOG_TAG, "Clicked Play");

        Toast.makeText(this, "Clicked Play!", Toast.LENGTH_SHORT).show();
    } else if (intent.getAction().equals(Constants.ACTION.NEXT_ACTION)) {
        Log.i(LOG_TAG, "Clicked Next");

        Toast.makeText(this, "Clicked Next!", Toast.LENGTH_SHORT).show();
    } else if (intent.getAction().equals(
            Constants.ACTION.STOPFOREGROUND_ACTION)) {
        Log.i(LOG_TAG, "Received Stop Foreground Intent");
        stopForeground(true);
        stopSelf();
    }
    return START_STICKY;
}

private void showNotification() {

    Intent previousIntent = new Intent(this, ForegroundService.class);
    previousIntent.setAction(Constants.ACTION.PREV_ACTION);
    PendingIntent ppreviousIntent = PendingIntent.getService(this, 0,
            previousIntent, 0);

    Intent playIntent = new Intent(this, ForegroundService.class);
    playIntent.setAction(Constants.ACTION.PLAY_ACTION);
    PendingIntent pplayIntent = PendingIntent.getService(this, 0,
            playIntent, 0);

    Intent nextIntent = new Intent(this, ForegroundService.class);
    nextIntent.setAction(Constants.ACTION.NEXT_ACTION);
    PendingIntent pnextIntent = PendingIntent.getService(this, 0,
            nextIntent, 0);




    startForeground(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE,
            setNotified("Notify","Title"));

}
private Notification setNotified(String title,String text)
{

    Intent notificationIntent = new Intent(this, Splash.class);
    notificationIntent.setAction(Constants.ACTION.MAIN_ACTION);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    Bitmap icon = BitmapFactory.decodeResource(getResources(),
            R.drawable.icon);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,              notificationIntent, 0);
   return new NotificationCompat.Builder(this)
           .setContentTitle(title)
           .setTicker(text)
           .setContentText(text)
           .setSmallIcon(R.drawable.icon)
           .setLargeIcon(Bitmap.createScaledBitmap(icon, 50, 50, false))
           .setContentIntent(pendingIntent)
           .setOngoing(true).build();
}
@Override
public void onDestroy()
{
    super.onDestroy();
    Log.i(LOG_TAG, "In onDestroy");
    Toast.makeText(this, "Service Detroyed!", Toast.LENGTH_SHORT).show();
}

@Override
public IBinder onBind(Intent intent) {
    // Used only in case if services are bound (Bound Services).
    return null;
}

最佳答案

你需要停止你的 Intent ,你可以在 onDestroy() 方法中这样做。

@Override
protected void onDestroy() {
// check for null pointer exception
    stopService(serviceIntent);
}

关于如果我使用 intent 进入主屏幕,Android 服务将被终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34217309/

相关文章:

android - 用于在 Android 上确认凭据的自定义屏幕

java - Centos java定制服务

symfony - Symfony2 服务的外观模式

Android - 后台服务在其调用程序 Activity 被销毁时停止

Android onHandleIntent 和 onStartCommand 的区别

java - 从命令行运行 Android Junit 测试

android - 尝试连接到数据库时出现 Web 服务错误

java - Android框架中AsyncTask的优缺点是什么?

android - Flutter UI 损坏了第一个屏幕

powershell - PowerShell 可以编写 SQL Server Reporting Services RDL 文件的脚本吗?