我的华为杀了我的wakelock安卓应用程序

我尝试开发使用手机加速度计的Android应用程序。 除了华为的手机(在P9上测试)之外,它无处不在。 我选中了“继续使用黑屏运行”选项并使用电池保护程序保护应用程序(电池选项)。

我看不到解决方案,所以我问你:-)这是我的活动:

public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; private MyService mService; private boolean mIsRunning; private MyService.ICallback mCallback = new MyService.ICallback() { public void changed() { Log.i(TAG, "CHANGED"); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override protected void onResume() { super.onResume(); if(!mIsRunning) { startStepService(); } bindStepService(); } @Override protected void onPause() { super.onPause(); if(mIsRunning && mService != null) { unbindStepService(); } } private ServiceConnection mConnection = new ServiceConnection() { public void onServiceConnected(ComponentName className, IBinder service) { mService = ((MyService.StepBinder)service).getService(); mService.registerCallback(mCallback); } public void onServiceDisconnected(ComponentName className) { mService = null; } }; private void startStepService() { if (! mIsRunning) { mIsRunning = true; startService(new Intent(MainActivity.this, MyService.class)); } } private void bindStepService() { bindService(new Intent(MainActivity.this, MyService.class), mConnection, Context.BIND_AUTO_CREATE); } private void unbindStepService() { unbindService(mConnection); } 

我的服务:

 public class MyService extends Service { private static final String TAG = "Test"; private SensorManager mSensorManager; private Detector detector; private MyDisplayer displayer; private PowerManager.WakeLock wakeLock; private ICallback mCallback; private MyDisplayer.Listener mStepListener = new MyDisplayer.Listener() { public void changed() { passValue(); } public void passValue() { if (mCallback != null) { mCallback.changed(); } } }; private final IBinder mBinder = new StepBinder(); public static final int MyID = 1234; android.app.Notification Notification; public class StepBinder extends Binder { public MyService getService() { return MyService.this; } } @Override public void onCreate() { super.onCreate(); acquireWakeLock(); displayer = new MyDisplayer(); displayer.addListener(mStepListener); registerDetector(); startServiceNotification(); } private void registerDetector() { mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); detector = new Detector(); detector.addStepListener(displayer); Sensor mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); mSensorManager.registerListener(detector, mSensor, SensorManager.SENSOR_DELAY_GAME); } @Override public IBinder onBind(Intent intent) { return mBinder; } public interface ICallback { void changed(); } public void registerCallback(ICallback cb) { mCallback = cb; } private void startServiceNotification() { NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.logo_32) .setContentTitle("Test") .setPriority(Notification.PRIORITY_HIGH) .setContentText("My Notif"); TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); Intent resultIntent = new Intent(this, MainActivity.class); stackBuilder.addNextIntent(resultIntent); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); mBuilder.setContentIntent(resultPendingIntent); startForeground(MyID, mBuilder.build()); } private void acquireWakeLock() { PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); int wakeFlags = PowerManager.PARTIAL_WAKE_LOCK; wakeLock = pm.newWakeLock(wakeFlags, TAG); wakeLock.acquire(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { return START_STICKY; } 

监听器和探测器对这个问题没用,所以我不把它们放在这里。 谢谢你的帮助。

您是否尝试在受保护的应用中列出您的应用? 请参阅华为手机上的“受保护的应用程序”设置,以及如何处理它 。 华为有一个特殊function,允许用户选择可以在后台运行的应用程序。 您可以要求用户接受您的应用在后台运行。 显然,你只能向他们展示适当的stetting页面,并要求他们自己启用此function。 另外我知道如果用户已经这样做,你无法以编程方式知道。