基本上我已经创建了警报应用程序但是没有在marshmallow中调用broadcastReceiver我知道marshmallow中所需的运行时权限但是我不知道调用broadcastReceiver的问题是什么。
请有人请你给我一些建议。
答案 0 :(得分:1)
我只是通过点击此链接创建服务来解决此问题
服务代码在这里......
import android.app.Service;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.support.annotation.IntDef;
import android.support.annotation.Nullable;
/**
* Created by waqar on 1/05/2017.
*/
public class LocalService extends Service {
AlarmReceiver alarmReceiver;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
alarmReceiver = new AlarmReceiver();
IntentFilter screenStateFilter = new IntentFilter();
screenStateFilter.addAction(Intent.ACTION_SCREEN_ON);
screenStateFilter.addAction(Intent.ACTION_SCREEN_OFF);
this.registerReceiver(alarmReceiver, screenStateFilter);
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(alarmReceiver);
}
}
在onCreate Method中从MainActivity启动此服务。
Intent intent = new Intent(getApplicationContext(), LocalService.class);
getApplicationContext().startService(intent);
不要忘记在清单中注册此服务。