我从我的主要Android活动开始提供服务,如下所示:
final Context context = base.getApplicationContext();
final Intent intent = new Intent(context, MyService.class);
startService(intent);
Run Code Online (Sandbox Code Playgroud)
当我通过从最近的应用程序列表中滑出来关闭活动页面时,服务会停止运行并在一段时间后重新启动.由于我的应用程序要求,我无法将持久服务与通知一起使用.如何使服务不重启或关闭,并继续在应用程序退出运行?
IntentService即使应用程序被杀,我也想继续在后台运行.并且被"杀死"我的意思是按住主页按钮很长时间 - > 查看所有正在运行的应用程序 - >将我的应用程序放在一边 - > 应用程序被杀或按下后退按钮很长时间 - > 应用程序被杀
我的代码如下.在我的MainActivity中:
Intent intent = new Intent(this, MyService.class);
this.startService(intent);
Run Code Online (Sandbox Code Playgroud)
在我的MyService中:
public class MyService extends IntentService {
@Override
protected void onHandleIntent(Intent intent) {
System.out.println("MyService started");
run();
}
private void run() {
while (true){
System.out.println("MyService still running");
doSomething();
waitSomeTime();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我看到应用程序打开时服务正在运行.当我通过主页按钮最小化应用程序时,它仍在运行.当我通过后退按钮关闭应用程序时,它仍在运行.但如果我如上所述杀死它,它就会停止.我该如何解决这个问题?
我有这个AndroidManifest.xml文件:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="1" android:versionName="1.0.0.0721"
android:process="com.lily.process" package="com.lily.test">
<provider android:authorities="com.lily.test"
android:name="com.lily.test.provider"
android:process="com.lily.process">
</provider>
Run Code Online (Sandbox Code Playgroud)
"android:process"作为清单标记和提供者标记添加,我知道如果它被添加为提供者标记,则提供者可以在"com.lily.process"过程中运行.但是当它作为清单标签编写时,它的用法是什么?我尝试过,但并非所有组件都可以在它识别的过程中运行.
我有一项服务,当用户更改他/她的位置时会发送通知.此服务工作正常,但是当用户关闭应用程序时服务关闭也会出现问题.
即使应用程序已关闭,我如何才能使服务仍然存在?
我的服务是:
public class LocationService extends Service implements LocationListener {
public final static int MINUTE = 1000 * 60;
boolean isGPSEnabled = false;
boolean isNetworkEnabled = false;
boolean canGetLocation = false;
Location location; // location
double latitude = 0; // latitude
double longitude = 0; // longitude
String provider;
// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = …Run Code Online (Sandbox Code Playgroud) 我正在使用Xamarin Android 3.5开发服务。我们的应用面向 Android 8.1 (API 27 - Oreo)。我希望该服务作为前台服务运行。但是,当我运行该服务时出现以下错误。
Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=null pri=1 contentView=null vibrate=null sound=null defaults=0x0 flags=0x42 color=0x00000000 vis=PRIVATE)
Run Code Online (Sandbox Code Playgroud)
这是服务的代码。
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
base.OnStartCommand(intent, flags, startId);
var context = Application.Context;
const int pendingIntentId = 0;
PendingIntent pendingIntent = PendingIntent.GetActivity(context, pendingIntentId, intent, PendingIntentFlags.OneShot);
var notification = new NotificationCompat.Builder(context)
.SetContentTitle("Testing")
.SetContentText("location tracking has begun.")
.SetSmallIcon(Resource.Drawable.icon)
.SetContentIntent(pendingIntent)
.SetOngoing(true)
.Build();
// Enlist this instance of the service …Run Code Online (Sandbox Code Playgroud) START_STICKY每当我杀死我的应用程序后就无法在我的设备上工作,而服务无法再次启动,我的设备名称为Redmi Note 3 Pro,但是每当我在android模拟器中运行相同的应用程序时,当我杀死该应用程序并服务直到我通过stopService()方法将其停止时才停止
请帮帮我
问题解决了
完成此操作:
设置>权限>自动启动, 然后打开我的应用程序的开关,然后完成!
我在此链接中找到了解决方案:解决方案链接