Java 当应用程序从Android中最近的应用程序中刷出时,服务停止
我正在制作一个非常简单的应用程序,我希望服务可以像Whatsapp服务在后台运行一样无限运行,即使该应用程序通过刷出从最近的应用程序中删除。Java 当应用程序从Android中最近的应用程序中刷出时,服务停止,java,android,performance,android-intent,android-service,Java,Android,Performance,Android Intent,Android Service,我正在制作一个非常简单的应用程序,我希望服务可以像Whatsapp服务在后台运行一样无限运行,即使该应用程序通过刷出从最近的应用程序中删除。 下面是我的代码 AndroidManifest.xml: <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.net.gs.servicetesting" > <application android:allo
下面是我的代码 AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.net.gs.servicetesting" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".MyService"
android:process=":com">
<intent-filter>
<action
android:name="com.net.gs.servicetesting.MyService" />
</intent-filter>
</service>
</application>
</manifest>
MainActivity.java:
public class MainActivity extends ActionBarActivity {
String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "Activity Strated");
startService(new Intent(MyService.class.getName()));
}
}
在服务#onstart命令
方法中,将起始意图存储到字段变量,比如minent
然后执行服务#onTaskRemoved
方法,如下所示:
@Override
@TargetApi(14)
public void onTaskRemoved(Intent rootIntent) {
startService(mIntent);
}
谢谢你的回复,但它仍然不起作用。当我关闭所有最近的应用程序(包括这一个)时,服务将停止。在清单中,添加您的服务android:stopWithTask=“false”属性。请将其作为答案发布。但我的服务类扩展了IntentService。现在我如何才能做到这一点?
@Override
@TargetApi(14)
public void onTaskRemoved(Intent rootIntent) {
startService(mIntent);
}