我无法在android后台运行服务 [英] I can not run service in android background

查看:106
本文介绍了我无法在android后台运行服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在android后台运行服务,但遇到了一些问题. 在Android Oreo版本上,我尝试在Android中使用AlarmService和StartForegroundService()运行无尽的服务.
但是我没有结果.

I am running service in android background, and I have some problem. Over Android Oreo version, I tried to run endless service using AlarmService and StartForegroundService() in android.
but I have got no result.

我的测试设备是Xiaomi,YunOS,Huawei和android版本是9.0、4.4和YunOS 5.1
首先,当mainactivity运行并随后关闭时,在onResume()onPause()函数中,我使用Receiver从startforegroundservice()startservice()函数开始了服务.
有一些带有以下内容的代码:

My testing device is Xiaomi, YunOS, Huawei and android version is 9.0, 4.4 and YunOS 5.1
At first, when mainactivity run and next close, in onResume() and onPause() function, I started service with startforegroundservice() and startservice() function using Receiver.
There are some codes with the followings:

- MainActivity

public void onResume() {
    mContext.stopService(new Intent(mContext, AppService.class));
}

public void onPause() {
    Intent intent = new Intent(mContext, RestartReceiver.class);
    intent.setAction("ACTION_REBOOT");
    mContext.sendBroadcast(intent);
}

-RestartReceiver

@Override
public void onReceive(Context context, Intent intent) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
    {
        context.startForegroundService(new Intent(context, 
        AppForegroundService.class));
    } else {
        context.startService(new Intent(context, AppService.class));
    }
}

- AppForegroundService



     @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            ToastUtil.showToast(this, "AppForegroundService onStartCommand");
            Log.d(TAG, "onStartCommand");
            startForeground(startId, getStartNotification());
            startService(new Intent(this, AppService.class));
            stopSelf();
            return START_STICKY;
        }

- AppService

@Override
public void onDestroy() {
    RestartAlarm();
    Log.d(TAG, "onDestroy: service done");
    ToastUtil.showToast(this, "onDestroy: service done");
}
private void RestartAlarm()
{
    ToastUtil.showToast(this, "Start AlarmService");
    Intent intent = new Intent(this, RestartReceiver.class);
    intent.setAction("ACTION_RESTART");
    sendBroadcast(intent);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, 
    intent, 0);
    long firstTime = SystemClock.elapsedRealtime();
    Log.d(TAG, "RestartAlarm: " + firstTime);
    firstTime += 1000;

    AlarmManager alarmManager = (AlarmManager) 
    getSystemService(Context.ALARM_SERVICE);
    alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 
    firstTime, 1000, pendingIntent);
}

- Manifest.xml

<service
    android:name=".service.AppService"
    android:enabled="true"
    android:exported="true"
    android:stopWithTask="false"/>
<service android:name=".service.AppForegroundService"
    android:enabled="true"
    android:exported="true"/>
<receiver
    android:enabled="true"
    android:exported="true"
    android:name=".service.AutoStartReceiver"
    android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
    <intent-filter>
        <action android:name="ACTION_REBOOT" />
        <action android:name="android.intent.action.BOOT_COMPLETED" 
        android:priority="999"/>
        </intent-filter>
</receiver>
<receiver
    android:name=".service.RestartReceiver"
    android:enabled="true">
    <intent-filter>
        <action android:name="ACTION_RESTART"/>
    </intent-filter>
</receiver>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" 
/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" 
/>
<uses-permission
    android:name="android.permission.READ_PHONE_STATE"
    android:required="false" />
<!-- 网络 -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" 
/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" 
/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission
    android:name="android.permission.ACCESS_FINE_LOCATION"
    android:required="false" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<!-- added from 2.7.2 -->
<uses-permission
    android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"
    android:required="false" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission 
android:name="android.permission.INSTANT_APP_FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission 
android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

<uses-permission android:name="android.permission.SET_ACTIVITY_WATCHER" 
/>
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.VIBRATE" />

我在做什么错了?

推荐答案

对于某些设备制造商不允许在应用程序关闭时不允许服务在后台运行的问题,您无能为力,因为操作系统会杀死该服务,但是您需要这样做如果确实需要在后台运行服务,请授予您的应用程序自启动权限!

You can do nothing about that some device manufacturers doesn't allow services to run in background when app is closed it's the os which kills the service however you need to give your app self start permission if it's really necessary for you to run service in background!

这篇关于我无法在android后台运行服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆