问题描述
当主应用程序正在运行但未激活时,单击服务通知即可将其置于最前面。 另一种情况是,通过从最近的应用程序列表中将其刷出来关闭该应用程序时,该服务会重新启动,并且当我单击通知时,仅显示黑屏,仅此而已。
我注意到的一件有趣的事情,在一段时间(一两个小时)后,它开始工作-单击服务通知,启动了主应用程序! 服务重新启动后如何实现?
该应用是使用Python + Kivy创建的,并使用Buildozer构建的,我在Android 10上运行该应用。
我尝试了setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
来实现意图,但没有帮助。
这是一些代码。 PythonService.java:
package org.kivy.android;
import android.os.Build;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
import android.app.Service;
import android.os.IBinder;
import android.os.Bundle;
import android.content.Intent;
import android.content.Context;
import android.util.Log;
import android.app.Notification;
import android.app.PendingIntent;
import android.os.Process;
import java.io.File;
//imports for channel deFinition
import android.app.notificationmanager;
import android.app.NotificationChannel;
import android.graphics.Color;
public class PythonService extends Service implements Runnable {
// Thread for Python code
private Thread pythonThread = null;
// Python environment variables
private String androidPrivate;
private String androidArgument;
private String pythonName;
private String pythonHome;
private String pythonPath;
private String serviceEntrypoint;
// Argument to pass to Python code,private String pythonServiceArgument;
public static PythonService mService = null;
private Intent startIntent = null;
private boolean autoRestartService = false;
public void setAutoRestartService(boolean restart) {
autoRestartService = restart;
}
public int startType() {
return START_NOT_STICKY;
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent,int flags,int startId) {
if (pythonThread != null) {
Log.v("python service","service exists,do not start again");
return START_NOT_STICKY;
}
startIntent = intent;
Bundle extras = intent.getExtras();
androidPrivate = extras.getString("androidPrivate");
androidArgument = extras.getString("androidArgument");
serviceEntrypoint = extras.getString("serviceEntrypoint");
pythonName = extras.getString("pythonName");
pythonHome = extras.getString("pythonHome");
pythonPath = extras.getString("pythonPath");
boolean serviceStartAsForeground = (
extras.getString("serviceStartAsForeground").equals("true")
);
pythonServiceArgument = extras.getString("pythonServiceArgument");
pythonThread = new Thread(this);
pythonThread.start();
if (serviceStartAsForeground) {
doStartForeground(extras);
}
return startType();
}
protected int getServiceId() {
return 1;
}
protected void doStartForeground(Bundle extras) {
String serviceTitle = extras.getString("serviceTitle");
String serviceDescription = extras.getString("serviceDescription");
Notification notification;
Context context = getApplicationContext();
Intent contextIntent = new Intent(context,PythonActivity.class);
contextIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // my attempt but it didn't helped
PendingIntent pIntent = PendingIntent.getActivity(context,contextIntent,PendingIntent.FLAG_UPDATE_CURRENT);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
notification = new Notification(
context.getApplicationInfo().icon,serviceTitle,System.currentTimeMillis());
try {
// prevent using NotificationCompat,this saves 100kb on apk
Method func = notification.getClass().getmethod(
"setLatestEventInfo",Context.class,CharSequence.class,PendingIntent.class);
func.invoke(notification,context,serviceDescription,pIntent);
} catch (NoSuchMethodException | illegalaccessexception |
IllegalArgumentException | InvocationTargetException e) {
}
} else {
// for android 8+ we need to create our own channel
// https://stackoverflow.com/questions/47531742/startforeground-fail-after-upgrade-to-android-8-1
String NOTIFICATION_CHANNEL_ID = "org.kivy.p4a"; //Todo: make this configurable
String channelName = "Background Service"; //Todo: make this configurable
NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID,channelName,notificationmanager.IMPORTANCE_NONE);
chan.setLightColor(Color.BLUE);
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
notificationmanager manager = (notificationmanager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.createNotificationChannel(chan);
Notification.Builder builder = new Notification.Builder(context,NOTIFICATION_CHANNEL_ID);
builder.setContentTitle(serviceTitle);
builder.setContentText(serviceDescription);
builder.setContentIntent(pIntent);
builder.setSmallIcon(context.getApplicationInfo().icon);
notification = builder.build();
}
startForeground(getServiceId(),notification);
}
@Override
public void onDestroy() {
super.onDestroy();
pythonThread = null;
if (autoRestartService && startIntent != null) {
Log.v("python service","service restart requested");
startService(startIntent);
}
Process.killProcess(Process.myPid());
}
/**
* Stops the task gracefully when killed.
* Calling stopSelf() will trigger a onDestroy() call from the system.
*/
@Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
stopSelf();
}
@Override
public void run(){
String app_root = getFilesDir().getAbsolutePath() + "/app";
File app_root_file = new File(app_root);
PythonUtil.loadLibraries(app_root_file,new File(getApplicationInfo().nativeLibraryDir));
this.mService = this;
nativeStart(
androidPrivate,androidArgument,serviceEntrypoint,pythonName,pythonHome,pythonPath,pythonServiceArgument);
stopSelf();
}
// Native part
public static native void nativeStart(
String androidPrivate,String androidArgument,String serviceEntrypoint,String pythonName,String pythonHome,String pythonPath,String pythonServiceArgument);
}
和扩展PythonService的类:
package org.test.schedules;
import android.content.Intent;
import android.content.Context;
import org.kivy.android.PythonService;
public class ServiceService extends PythonService {
@Override
protected int getServiceId() {
return 1;
}
static public void start(Context ctx,String pythonServiceArgument) {
Intent intent = new Intent(ctx,ServiceService.class);
String argument = ctx.getFilesDir().getAbsolutePath() + "/app";
intent.putExtra("androidPrivate",ctx.getFilesDir().getAbsolutePath());
intent.putExtra("androidArgument",argument);
intent.putExtra("serviceTitle","Schedules1106fgs_sdk28");
intent.putExtra("serviceDescription","Service");
intent.putExtra("serviceEntrypoint","service/main.py");
intent.putExtra("pythonName","service");
intent.putExtra("serviceStartAsForeground","true");
intent.putExtra("pythonHome",argument);
intent.putExtra("pythonPath",argument + ":" + argument + "/lib");
intent.putExtra("pythonServiceArgument",pythonServiceArgument);
ctx.startService(intent);
}
static public void stop(Context ctx) {
Intent intent = new Intent(ctx,ServiceService.class);
ctx.stopService(intent);
}
}
这也是AndroidManifest.xml的一部分
<activity android:name="org.kivy.android.PythonActivity"
android:label="@string/app_name"
android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|fontScale|uiMode|uiMode|screenSize|smallestScreenSize|layoutDirection"
android:screenorientation="portrait"
android:launchMode="singleTask"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="org.kivy.android.PythonService"
android:process=":pythonservice" />
<service android:name="org.test.schedules.ServiceService"
android:process=":service_service" />
将非常感谢您的帮助
更新
我还没有找到解决方案,但是注意到了,但是问题似乎与后台缓存过程有关。因此,如果我转到开发人员选项->运行服务->显示缓存的进程并在那里手动停止该进程,则该应用程序可以正常重启。 我想该解决方案可能会以某种方式阻止在后台创建缓存的进程或在某个特定时刻终止该进程,在应用退出或服务重启时,我不是Android方面的专家,有人可以帮忙吗? >
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)