0

嗨,我有这项服务可以在后台运行通知:

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>

但是服务没有启动..我只希望电池电量的通知在后台运行但现在不工作..我在哪里错了?

4

1 回答 1

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);
于 2013-08-12T00:01:19.257 回答