正如标题,我刚刚使用 Unbound Service 构建了一个应用程序,但它在 Lenovo A6000 上运行不正常,当我杀死应用程序时,我之前启动的服务也被关闭了。 但是 Facebook 中的服务、Messenger 应用程序可以在此设备上正常运行。 那么,我该怎么办呢?
最佳答案
嗨,Nam Nguyen & ADM,这里创建的服务在终止应用程序后在我的联想设备后台运行,
这里需要创建服务,一个是后台任务,一个是报警服务,在一段时间后不断检查你的服务,
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
/**
* Created by DhavalSolanki on 30/3/17.
*/
public class ConnectionService extends Service {
private final String TAG = getClass().getSimpleName();
private Context context;
@Override
public IBinder onBind(Intent intent) {
Log.i(TAG, "onBind");
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
context = getApplicationContext();
return Service.START_STICKY;
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public boolean onUnbind(Intent intent) {
Log.i(TAG, "onUnbind");
return super.onUnbind(intent);
}
@Override
public void onDestroy() {
Log.i(TAG, "onDestroy");
super.onDestroy();
}
}
import android.app.ActivityManager;
import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
/**
* Created by DhavalSolanki on 30/3/17.
*/
public class AlarmService extends IntentService {
@Override
protected void onHandleIntent(Intent intent) {
boolean isServiceRunning = false;
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (ConnectionService.class.getName().equals(service.service.getClassName())) {
isServiceRunning = true;
break;
}
}
// Logger.print("AlarmService", "Service is running " + isServiceRunning);
if (!isServiceRunning) {
startService(new Intent(getApplicationContext(), ConnectionService.class));
}
}
/**
* Creates an IntentService. Invoked by your subclass's constructor.
*/
public AlarmService() {
super("Alarm");
}
}
在开始服务的地方写下面的代码
private void checkAndStartService() {
final ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
new AsyncTask<Void, Void, Boolean>() {
boolean isServiceRunning = false;
@Override
protected Boolean doInBackground(Void... params) {
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (ConnectionService.class.getName().equals(service.service.getClassName())) {
isServiceRunning = true;
break;
}
}
return isServiceRunning;
}
@Override
protected void onPostExecute(Boolean aBoolean) {
super.onPostExecute(aBoolean);
Log.i("onPostExecute", "Service running = " + aBoolean);
if (!aBoolean) {
startService(new Intent(ActivitySplash.this, ConnectionService.class));
Intent i = new Intent(ActivitySplash.this, AlarmService.class);
PendingIntent pi = PendingIntent.getService(ActivitySplash.this, 0, i, 0);
AlarmManager am = (AlarmManager) ActivitySplash.this.getSystemService(Context.ALARM_SERVICE);
am.cancel(pi); // cancel any existing alarms
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 10000, pi);
}
}
}.execute();
}
最后在 list 文件中注册这两个服务
<service android:name=".services.ConnectionService"
android:enabled="true"
android:exported="false"
android:stopWithTask="false" />
<service
android:name=".services.AlarmService"
android:enabled="true" />
关于android - 联想A600在应用程序被杀死后无法保持服务运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43108822/