如何在Android Top 5上使用该服务

时间:2017-06-15 05:23:50

标签: service android-service android-5.0-lollipop android-6.0-marshmallow android-wake-lock

我在我的计划中使用了service,但只要服务无效top 5 Android手机就会睡眠。< / p>

它适用于Android 4而没有任何问题。

请帮帮我。

  

在活动中

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

       startService(new Intent(this , MyService.class));
  }
}
  

在服务中

public class MyService extends android.app.Service {

private PowerManager pm;
private WifiManager wm;

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

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    //do something code

    return START_STICKY;
}

@Override
public void onTaskRemoved(Intent rootIntent) {
    if (android.os.Build.VERSION.SDK_INT >= 21) {
        pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
        final PowerManager.WakeLock wakeLock = pm.newWakeLock((PowerManager.PARTIAL_WAKE_LOCK), "TAG");
        wakeLock.acquire();

        wm = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        WifiManager.WifiLock lock = wm.createWifiLock(WifiManager.WIFI_MODE_FULL, "LockTag");
        lock.acquire();

        Intent restartServiceIntent = new Intent(getApplicationContext(),
                MyService.class);
        restartServiceIntent.setPackage(getPackageName());

        PendingIntent restartServicePendingIntent = PendingIntent.getService(
                getApplicationContext(), 0, restartServiceIntent,
                PendingIntent.FLAG_ONE_SHOT);
        AlarmManager alarmService = (AlarmManager) getApplicationContext()
                .getSystemService(Context.ALARM_SERVICE);

        long thirtySecondsFromNow = 1000;
        alarmService.set(AlarmManager.RTC_WAKEUP, thirtySecondsFromNow, restartServicePendingIntent);

        ComponentName receiver = new ComponentName(getApplicationContext(), MyService.class);
        PackageManager pm = getApplicationContext().getPackageManager();
        pm.setComponentEnabledSetting(receiver,
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                PackageManager.DONT_KILL_APP);



        super.onTaskRemoved(rootIntent);
    }
}
}
  

在Manifest.xml中

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="app.service">

<uses-permission android:name="android.permission.WAKE_LOCK" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service
        android:name=".MyService"
        android:enabled="true"
        android:exported="false"
        android:label="my"
        android:largeHeap="true"
        android:stopWithTask="false" />

</application>

0 个答案:

没有答案