我的华为杀死了我的wakelock安卓应用

时间:2016-10-20 12:00:28

标签: android kill android-wake-lock huawei

我尝试开发使用手机加速度计的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;
}

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

1 个答案:

答案 0 :(得分:0)

您是否尝试在受保护的应用中列出您的应用?见"Protected Apps" setting on Huawei phones, and how to handle it。华为有一项特殊功能,允许用户选择可以在后台运行的应用程序。您可以要求用户接受您的应用在后台运行。显然,你只能向他们展示适当的剪辑页面,并要求他们自己启用此功能。另外我知道如果用户已经这样做,你无法以编程方式知道。