Reputation: 1402
Hi i have this service to run in background a notification:
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);
}
}
};
}
And this is the receiver that "calls" the service
public class MyscheduleReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent){
Intent service = new Intent (context, service.class);
context.startService(service);
}
}
And the manifest:
<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>
But the service doesn't start..I only want that the notification with the battery level runs in background but not working now..Where i'm wronging?
Upvotes: 0
Views: 250
Reputation: 770
From the android documentation regarding the ACTION_BATTERY_CHANGED intent:
You can not receive this through components declared in manifests, only by explicitly registering for it with Context.registerReceiver(). See ACTION_BATTERY_LOW, ACTION_BATTERY_OKAY, ACTION_POWER_CONNECTED, and ACTION_POWER_DISCONNECTED for distinct battery-related broadcasts that are sent and can be received through manifest receivers.
So, if you wish to receive broadcasts from this intent you will need to create a receiver programmatically:
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = context.registerReceiver(null, ifilter);
Upvotes: 2