2016-10-31 67 views
0

我試着寫一個應用程序來監控手機的充電水平,所以我試圖建立不可阻擋的服務。但現在,當我關閉應用程序時,它沒有發送任何通知。不可阻擋的服務

這裏是我的代碼:

AndroidManifest.xml中

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETE"/> 
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> 

<service 
     android:name=".NotificationService" 
     android:enabled="true" 
     android:exported="true" 
     android:permission="android.permission.RECEIVE_BOOT_COMPLETE" 
     android:process=":BatMonitoring"> 
    </service> 

    <receiver 
     android:name=".BootReceiver" 
     android:label="@string/app_name" 
     android:permission="android.permission.RECEIVE_BOOT_COMPLETED" 
     android:enabled="true"> 

     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED" /> 
      <action android:name="android.intent.action.QUICKBOOT_POWERON" /> 
      <action android:name="android.intent.action.REBOOT"/> 
      <action android:name="android.intent.action.ACTION_POWER_CONNECTED"/> 
      <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/> 

      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 

    </receiver> 

MainActivity.java(代碼主要部分)

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    batteryLevel = (TextView)findViewById(R.id.battery_level); 
    voltageLevel = (TextView)findViewById(R.id.voltage_level); 
    temperatureLevel = (TextView)findViewById(R.id.temperature_level); 

    registerReceiver(this.mBatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); 

    Intent intentService = new Intent(this,NotificationService.class); 
    startService(intentService); 
} 

BootReceiver.java

public class BootReceiver extends BroadcastReceiver { 

@Override 
public void onReceive(Context context, Intent intent) { 
     Intent serviceIntent = new Intent(context, NotificationService.class); 
     context.startService(serviceIntent); 
} 

}

NotificationService.java

public class NotificationService extends Service implements Constants { 

private String title; 
private String msg; 

@Override 
public void onCreate() { 
    super.onCreate(); 

    this.registerReceiver(this.mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); 

} 


public int onStartCommand(Intent intent, int flags, int startId) { 

    sendNotif(START_MSG1,START_MSG2); 
    return START_STICKY; 

} 

    BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); 
      int chargeState = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1); 
      boolean isDischarging = chargeState == BatteryManager.BATTERY_STATUS_DISCHARGING; 

      if (isDischarging && title!=null && msg!=null) { 


        switch(level){ 

         //some other not important code 

        } 
       sendNotif(title, msg); 

      } 

     } 

    }; 

private void sendNotif(String title, String msg) { 
    NotificationCompat.Builder builder = new NotificationCompat.Builder(NotificationService.this) 
      .setContentTitle(title) 
      .setContentText(msg) 
      .setSmallIcon(android.R.drawable.stat_notify_chat); 

    Notification notification = builder.build(); 
    notification.defaults |= Notification.DEFAULT_VIBRATE; 
    notification.defaults |= Notification.DEFAULT_SOUND; 

    startForeground(DEFAULT_NOTIFICATION_ID, notification); 
} 




@Override 
    public void onDestroy() { 
     super.onDestroy(); 

     this.unregisterReceiver(this.mBatteryInfoReceiver); 

     startService(new Intent(this,NotificationService.class)); 

     //Disabling service 
     //stopSelf(); 
    } 

@Override 
public void onTaskRemoved(Intent rootIntent) { 
    super.onTaskRemoved(rootIntent); 
    startService(new Intent(this,NotificationService.class)); 
} 


public IBinder onBind(Intent intent) { 
    return null; 
} 

}

會明白任何幫助,因爲我不能這個問題應付更多的則一週。

+1

「不可阻擋的服務」不存在,即使可能,您也會打亂用戶。接近這樣一個唯一的方式是處理打盹模式,我想這是你的服務停止.https://developer.android.com/training/monitoring-device-state/doze-standby.html – Opiatefuchs

+0

@ Opiatefuchs,但有很多像facebook,viber,甚至是鬧鐘等應用程序,都可以在任何時候在前臺工作。 –

+0

不,他們不是,他們只是在做一個小把戲。這是一個巨大的話題,尤其是從MM打瞌睡模式開始。它以您在服務的onStartCommand中返回的內容開始,請參閱文檔https://developer.android.com/reference/android/app/Service.html#START_STICKY – Opiatefuchs

回答

0

我相信你正在尋找一個後臺服務而不是一個應用程序。官方文檔可以找到here

後臺服務沒有用戶界面。

Here is a good tutorial