7

I try to develop app for Android which uses phone's accelerometer. It works everywhere except on Huawei's phone (testing on P9). I checked option "keep running with black screen" and protect application with battery saver (battery's option).

I can't see the solution, so I ask to 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;
}

Listener and Detector are useless for this problem so I don't put them here. Thank you for you help.

Leo
  • 81
  • 7

1 Answers1

0

Have you tried listing your app among the protected apps? See "Protected Apps" setting on Huawei phones, and how to handle it. Huawei have a special feature that allow users to select which applications can run in the background. You can ask the user to accept that your app runs in the background. Apparently however you can only show them the appropriate stetting page and ask that they enable this feature themselves. Also I understand you do not have any way to know programmatically if the user has done so.

Community
  • 1
  • 1
FabioC
  • 462
  • 5
  • 14
  • Thx for your answer. This "protect" in settings, worked for few hours but after, Huawei has killed app :) – Leo Feb 09 '17 at 14:32
  • Have you seen this [http://stackoverflow.com/questions/39954822/battery-optimizations-wakelocks-on-huawei-emui-4-0](post)? The person claims that the issue is the use of the wakelock. I suggested to him to stop it for a few seconds. I am however testing a similar program without a wakelock. The phone does not seem to need it – FabioC Feb 11 '17 at 18:05