嗨,我有这项服务可以在后台运行通知:
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>
但是服务没有启动..我只希望电池电量的通知在后台运行但现在不工作..我在哪里错了?