后,这是服务,我想在后台创建即使应用程序被终止
public class CustomMyService extends Service {
public CustomMyService() {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onTaskRemoved(Intent rootIntent) {
Intent intent = new Intent("com.android.ServiceStopped");
sendBroadcast(intent);
}
}
清单文件运行于运行Android应用程序服务
<service android:name=".CustomMyService">
<intent-filter android:priority="1000">
<action android:name="android.location.PROVIDERS_CHANGED"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</service>
敬请有人让我知道。我做了所有谷歌和YouTube的搜索,没有任何工作。
我使用红米注3,其中我有一个选项称为自动启动,如果我给这个应用程序的自动启动权限,服务在后台运行。
但是这个选项在许多其他的android智能手机中并不存在,所以应用程序终止时应用程序服务将被终止。
请让我知道服务如何运行,即使应用程序终止。
写一个私有的类方法与你希望你的服务来执行,并从'onStartCommand称之为()'的代码。除非需要某些东西来初始化服务,否则构造函数不是必需的。 –