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 に答える