I am trying to develop an Android application that uses the phone’s accelerometer. It works everywhere except for a Huawei phone (tested on P9). I checked the "continue to work with black screen" option and protect the application with a battery (battery option).
I do not see a solution, so I ask you :-) This is my activity:
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); }
And my service:
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; }
The listener and detector are useless for this problem, so I do not put them here. Thanks for helping.
android huawei kill android-wake-lock
Leo
source share