永不停息的服务
Unstoppable service
我尝试编写一个应用程序来一直监控 phone 的充电水平,因此我尝试构建不间断的服务。但是现在,当我关闭应用程序时,它没有向我发送任何通知。
这是我的代码:
AndroidManifest.xml
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<service
android:name=".NotificationService"
android:enabled="true"
android:exported="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETE"
android:process=":BatMonitoring">
</service>
<receiver
android:name=".BootReceiver"
android:label="@string/app_name"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="android.intent.action.REBOOT"/>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
MainActivity.java(主要代码部分)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
batteryLevel = (TextView)findViewById(R.id.battery_level);
voltageLevel = (TextView)findViewById(R.id.voltage_level);
temperatureLevel = (TextView)findViewById(R.id.temperature_level);
registerReceiver(this.mBatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Intent intentService = new Intent(this,NotificationService.class);
startService(intentService);
}
BootReceiver.java
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent serviceIntent = new Intent(context, NotificationService.class);
context.startService(serviceIntent);
}
}
NotificationService.java
public class NotificationService extends Service implements Constants {
private String title;
private String msg;
@Override
public void onCreate() {
super.onCreate();
this.registerReceiver(this.mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}
public int onStartCommand(Intent intent, int flags, int startId) {
sendNotif(START_MSG1,START_MSG2);
return START_STICKY;
}
BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int chargeState = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isDischarging = chargeState == BatteryManager.BATTERY_STATUS_DISCHARGING;
if (isDischarging && title!=null && msg!=null) {
switch(level){
//some other not important code
}
sendNotif(title, msg);
}
}
};
private void sendNotif(String title, String msg) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(NotificationService.this)
.setContentTitle(title)
.setContentText(msg)
.setSmallIcon(android.R.drawable.stat_notify_chat);
Notification notification = builder.build();
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_SOUND;
startForeground(DEFAULT_NOTIFICATION_ID, notification);
}
@Override
public void onDestroy () {
super.onDestroy();
this.unregisterReceiver(this.mBatteryInfoReceiver);
startService(new Intent(this,NotificationService.class));
//Disabling service
//stopSelf();
}
@Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
startService(new Intent(this,NotificationService.class));
}
public IBinder onBind(Intent intent) {
return null;
}
}
将不胜感激任何帮助,因为我无法解决这个问题超过一周。
我相信您正在寻找 后台服务 而不是应用程序。可以找到官方文档 here.
后台服务没有用户界面。
我尝试编写一个应用程序来一直监控 phone 的充电水平,因此我尝试构建不间断的服务。但是现在,当我关闭应用程序时,它没有向我发送任何通知。
这是我的代码:
AndroidManifest.xml
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<service
android:name=".NotificationService"
android:enabled="true"
android:exported="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETE"
android:process=":BatMonitoring">
</service>
<receiver
android:name=".BootReceiver"
android:label="@string/app_name"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="android.intent.action.REBOOT"/>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
MainActivity.java(主要代码部分)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
batteryLevel = (TextView)findViewById(R.id.battery_level);
voltageLevel = (TextView)findViewById(R.id.voltage_level);
temperatureLevel = (TextView)findViewById(R.id.temperature_level);
registerReceiver(this.mBatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Intent intentService = new Intent(this,NotificationService.class);
startService(intentService);
}
BootReceiver.java
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent serviceIntent = new Intent(context, NotificationService.class);
context.startService(serviceIntent);
}
}
NotificationService.java
public class NotificationService extends Service implements Constants {
private String title;
private String msg;
@Override
public void onCreate() {
super.onCreate();
this.registerReceiver(this.mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}
public int onStartCommand(Intent intent, int flags, int startId) {
sendNotif(START_MSG1,START_MSG2);
return START_STICKY;
}
BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int chargeState = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isDischarging = chargeState == BatteryManager.BATTERY_STATUS_DISCHARGING;
if (isDischarging && title!=null && msg!=null) {
switch(level){
//some other not important code
}
sendNotif(title, msg);
}
}
};
private void sendNotif(String title, String msg) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(NotificationService.this)
.setContentTitle(title)
.setContentText(msg)
.setSmallIcon(android.R.drawable.stat_notify_chat);
Notification notification = builder.build();
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_SOUND;
startForeground(DEFAULT_NOTIFICATION_ID, notification);
}
@Override
public void onDestroy () {
super.onDestroy();
this.unregisterReceiver(this.mBatteryInfoReceiver);
startService(new Intent(this,NotificationService.class));
//Disabling service
//stopSelf();
}
@Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
startService(new Intent(this,NotificationService.class));
}
public IBinder onBind(Intent intent) {
return null;
}
}
将不胜感激任何帮助,因为我无法解决这个问题超过一周。
我相信您正在寻找 后台服务 而不是应用程序。可以找到官方文档 here.
后台服务没有用户界面。