简体   繁体   English

从Android中的最新应用中刷出应用后,服务停止

[英]Service stopped when app is swiped out from recent apps in Android

I am making a very simple app in which I want that a service should run infinitely just like Whatsapp service runs in background, even if the app is removed from Recent Apps by swiping out. 我正在制作一个非常简单的应用程序,在该应用程序中,我希望服务可以像Whatsapp服务在后台运行一样无限运行,即使该应用通过刷卡从“最近的应用程序”中删除也是如此。
Below is my code. 下面是我的代码。

AndroidManifest.xml: 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>

MyService.java: MyService.java:

public class MyService extends Service {
    private String TAG = "MyService";

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG,"Service Strated");
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        Log.d(TAG,"Service Destoryed");
        super.onDestroy();
    }
}

MainActivity.java: 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()));
    }
}

In your Service#onStartCommand method store the starting intent to a field variable, let's say mIntent . 在您的Service#onStartCommand方法中,将开始意图存储到字段变量中,比如说mIntent

Then implement the Service#onTaskRemoved method like this: 然后像这样实现Service#onTaskRemoved方法:

@Override
@TargetApi(14)
public void onTaskRemoved(Intent rootIntent) {
    startService(mIntent);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当应用程序从android中的最新应用程序列表中删除时,服务停止 - Service stopped when the app removes from recent apps list in android Android - 从最近的应用程序中滑动应用程序时停止服务 - Android - Service stops when swiping app from recent apps 在android生命周期中,当从最近的应用列表中滑动应用时,是否会调用任何独特的方法? - In android lifecycle, is there any unique method gets called when app is swiped from recent app list? 在一加3t移动设备上杀死应用程序或从最近使用的应用程序中刷出应用程序时,Android FCM推送通知不起作用 - Android FCM Push Notification Not Working When App is being Killed or Swipe Out from Recent Apps on One plus 3t Mobile 在China Phone中刷新应用程序后台服务已停止 - Application background service stopped when swiped up in China Phone 从“最近使用的应用程序”列表中删除应用程序后,Android服务将重新启动 - Android service gets restarted when application is removed from Recent Apps list Android:从最近使用的应用程序按钮关闭应用程序时未调用OnDestroy - Android: OnDestroy isn't called when I close the app from the recent apps button 从最近的应用程序中删除应用程序后,IntentService停止工作 - IntentService stops working when app is removed from recent apps 从最新信息中清除应用程序后,CountDownTimer无法在服务中运行吗? - CountDownTimer does not run in service after app is swiped from recents? 是否可以通过在 Android 上使用 Appium 滑动来从最近的应用程序屏幕中杀死一个应用程序? - Is it possible to kill an app from recent apps screen by swiping using Appium on Android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM