开机后无法启动Android前台服务

时间:2019-05-22 14:10:06

标签: android android-intent

我有一个前台服务,该服务应该在重新启动后可以工作。重新启动有3个版本。 a)用户单击重新启动按钮。 b)用户单击电源按钮,然后在一定时间后单击电源按钮 c)电池没电了。用户已充电并启动了电话。 我的代码适用于a)和b),没有任何问题。对于c),它会工作几次,然后无法启动。 Google商店上没有崩溃消息。如果我在手机上清除了我的应用程序的数据,然后重新启动了手机,则我的服务将启动。我猜是电池没电了,有时可能没有调用onDestroy服务,但是我不明白它如何阻止我的服务启动。

 The following manifest.
<?xml version="1.0" encoding="UTF-8"?>
<manifest package="bla.bla.bla.bla"   
 xmlns:android="http://schemas.android.com/apk/res/android">
 <uses-permission 
 android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
 <uses-permission 
 android:name="android.permission.ACCESS_FINE_LOCATION"/>
 <uses-permission 
 android:name="android.permission.ACCESS_COARSE_LOCATION"/>
 <uses-permission 
 android:name="android.permission.ACCESS_NETWORK_STATE"/>
 <uses-permission android:name="android.permission.INTERNET"/>
 <uses-permission android:name="android.permission.WAKE_LOCK"/>
 <application android:theme="@style/AppTheme" android:supportsRtl="true" 
 android:roundIcon="@mipmap/ic_launcher_round" 
 android:label="@string/app_name" android:icon="@mipmap/ic_launcher" 
 android:allowBackup="true">
 <activity android:name=".MainActivity" 
 android:theme="@style/AppTheme.NoActionBar" 
 android:label="@string/title_activity_main">
 <intent-filter>
 <action android:name="android.intent.action.MAIN"/>
 <category android:name="android.intent.category.LAUNCHER"/>
 </intent-filter>
 </activity>
 <service android:name="bla.bla.bla.bla.AndroidLocationServices"/>
 <receiver android:name="AlarmReceiver">
 <intent-filter>
 <action android:name="android.intent.action.BOOT_COMPLETED"/>
 </intent-filter>
 </receiver>
 </application>
 </manifest>

 The following class starts my service after boot. 
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
 import android.os.Build;
 public class AlarmReceiver extends BroadcastReceiver {
 public AlarmReceiver() {
    // TODO Auto-generated constructor stub
 }
 @Override
 public void onReceive(Context context, Intent arg1) {
 Toast.LENGTH_SHORT).show();
    //context.startService(new Intent(context, 
 AndroidLocationServices.class));
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        context.startForegroundService(new Intent(context, 
 AndroidLocationServices.class));
    else
        context.startService(new Intent(context, 
 AndroidLocationServices.class));
 }
 }
 The following some methods of my service class. 
 @Override
 public void onCreate() {
     super.onCreate();
     PowerManager pm = (PowerManager) 
 getSystemService(this.POWER_SERVICE);
    try {
        wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, 
"tag:DoNotSleep");
        wakeLock.acquire();
    }
    catch (NullPointerException exception)
    {
    }     
 }
 @Override
 public int onStartCommand(Intent intent, int flags, int startId) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        Notification.Builder builder = new Notification.Builder(this, 
 "MY_CHANNEL")
                .setContentTitle(getString(R.string.app_name))
                .setContentText("MY TRACK")
                .setAutoCancel(true);
        Notification notification = builder.build();
        startForeground(1, notification);
    } else {
        NotificationCompat.Builder builder = (NotificationCompat.Builder) 
 new NotificationCompat.Builder(this)
                .setContentTitle(getString(R.string.app_name))
                .setContentText("MY TRACK")
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setAutoCancel(true);
        Notification notification = builder.build();
        startForeground(1, notification);
    }
    locationManager = (LocationManager) getApplicationContext()
            .getSystemService(Context.LOCATION_SERVICE);
       if (ActivityCompat.checkSelfPermission(this, 
 Manifest.permission.ACCESS_FINE_LOCATION) != 
 PackageManager.PERMISSION_GRANTED && 
 ActivityCompat.checkSelfPermission(this, 
 Manifest.permission.ACCESS_COARSE_LOCATION) != 
 PackageManager.PERMISSION_GRANTED)
         return START_STICKY;
    }
    //Here appropriate code is placed. It is deleted.
     return START_STICKY;
    }
  @Override
  public void onDestroy() {
    super.onDestroy();
    if ((wakeLock!=null)&&(wakeLock.isHeld()))
    wakeLock.release();
  }

 No error messages.

0 个答案:

没有答案