gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:42:43 27 4
gpt4 key购买 nike

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

这是我在退出按钮上写的

                 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com