您好我有这项服务在后台运行通知:
public class service extends Service {
NotificationManager mNotificationManager;
public SharedPreferences preferences;
public SharedPreferences myPref;
// Notifications//
private static final int SIMPLE_NOTIFICATION_ID = 1;
private static final String PREFS_NAME = "MyPreferences";
SharedPreferences mprefs;
@Override
public IBinder onBind(Intent intent){
return null;
}
@Override
public void onCreate(){
super.onCreate();
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
new IntentFilter(Intent.ACTION_BATTERY_LOW);
mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
}
@Override
public void onDestroy(){
super.onDestroy();
}
@Override
public void onStart(Intent intent, int startId){
this.registerReceiver(this.batteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}
BroadcastReceiver batteryInfoReceiver = new BroadcastReceiver() {
// Gestistco tutte le variabili da dove prelevo i dati della batteria //
@SuppressLint("NewApi")
@Override
public void onReceive(Context context, Intent intent) {
mprefs = getSharedPreferences(PREFS_NAME, 0);
int level= intent.getIntExtra(BatteryManager.EXTRA_LEVEL,-1);
intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
intent.getAction().equals(Intent.ACTION_BATTERY_LOW);
intent.getIntExtra(BatteryManager.EXTRA_PLUGGED,-1);
intent.getExtras().getBoolean(BatteryManager.EXTRA_PRESENT);
intent.getIntExtra(BatteryManager.EXTRA_SCALE,0);
intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
intent.getExtras().getString(BatteryManager.EXTRA_TECHNOLOGY);
int temperature= intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE,-1)/10;
intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE,-1);
Log.i("BR", "onReceiveS");
if(mprefs.getBoolean("notification_a", false)!=false){
mNotificationManager=(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context);
notificationBuilder.setOngoing(true);
notificationBuilder.setContentTitle("Battery Stats Informations");
notificationBuilder.setContentText("Carica residua: " +level+"%" + " " + "Temperatura: " +temperature+ "°C");
//notificationBuilder.setTicker("Informazioni batteria");
notificationBuilder.setWhen(System.currentTimeMillis());
notificationBuilder.setSmallIcon(R.drawable.icon_small_not);
Intent notificationIntent = new Intent(context, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getBroadcast(context, 0, notificationIntent, 0);
notificationBuilder.setContentIntent(contentIntent);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Notification not=notificationBuilder.build();
not.flags|=Notification.FLAG_FOREGROUND_SERVICE;
mNotificationManager.notify(SIMPLE_NOTIFICATION_ID,not);
}
}
};
}
这是“调用”服务的接收者
public class MyscheduleReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent){
Intent service = new Intent (context, service.class);
context.startService(service);
}
}
清单:
<receiver android:name="com.pak.myapp.MyscheduleReceiver" >
<intent-filter>
<action android:name="android.intent.action.BATTERY_CHANGED" />
</intent-filter>
</receiver>
<service android:name="service">
</service>
但该服务无法启动..我只希望电池级别的通知在后台运行但现在不能正常工作..我在哪里做错了?
答案 0 :(得分:2)
关于ACTION_BATTERY_CHANGED意图的Android文档:
只有通过使用Context.registerReceiver()显式注册它,才能通过清单中声明的组件来接收它。请参阅ACTION_BATTERY_LOW,ACTION_BATTERY_OKAY,ACTION_POWER_CONNECTED和ACTION_POWER_DISCONNECTED,了解发送并可通过清单接收器接收的不同电池相关广播。
因此,如果您希望从此意图接收广播,则需要以编程方式创建接收器:
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = context.registerReceiver(null, ifilter);