2013-07-29 59 views
3

我想建立一個功能,根據電池的百分比推動通知。Android - 推送通知按電池的百分比,而不啓動應用程序

情況1: 電池的百分比在30%到40%的範圍內下降,通知欄中會彈出一條消息,通知用戶電池電量不足。

我試圖做一個服務,但無濟於事。我能夠讓BroadcastReceiver檢索電池的百分比並彈出通知,但我無法讓服務得以解決。

我有兩個班,MainActivity和BatteryService

這就是我把我的BatteryService類

public class BatteryService extends Service { 

int level; 
public BroadcastReceiver BatInfoReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context arg0, Intent intent) { 
     level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); 

     if (level == 50) 
     { 
      // Notify the user with notification 
     } 
    } 
}; 

public void onCreate() { 
    super.onCreate(); 
    this.registerReceiver(this.BatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); 
} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    Toast.makeText(getApplicationContext(), "Command=" + level + "%", Toast.LENGTH_LONG).show(); 
    stopSelf(); 
    return START_STICKY; 
} 

@Override 
public void onDestroy() { 
    super.onDestroy(); 
    unregisterReceiver(BatInfoReceiver); 
} 

@Override 
public IBinder onBind(Intent arg0) { 
    // TODO Auto-generated method stub 
    return null; 
} 

我得到了這樣的錯誤

07-29 03:08:49.447: E/AndroidRuntime(1204): FATAL EXCEPTION: main 
07-29 03:08:49.447: E/AndroidRuntime(1204): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.BATTERY_CHANGED flg=0x60000010 (has extras) } in [email protected] 
07-29 03:08:49.447: E/AndroidRuntime(1204):  at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:765) 
07-29 03:08:49.447: E/AndroidRuntime(1204):  at android.os.Handler.handleCallback(Handler.java:615) 
07-29 03:08:49.447: E/AndroidRuntime(1204):  at android.os.Handler.dispatchMessage(Handler.java:92) 
07-29 03:08:49.447: E/AndroidRuntime(1204):  at android.os.Looper.loop(Looper.java:137) 
07-29 03:08:49.447: E/AndroidRuntime(1204):  at android.app.ActivityThread.main(ActivityThread.java:4745) 
07-29 03:08:49.447: E/AndroidRuntime(1204):  at java.lang.reflect.Method.invokeNative(Native Method) 
07-29 03:08:49.447: E/AndroidRuntime(1204):  at java.lang.reflect.Method.invoke(Method.java:511) 
07-29 03:08:49.447: E/AndroidRuntime(1204):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
07-29 03:08:49.447: E/AndroidRuntime(1204):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
07-29 03:08:49.447: E/AndroidRuntime(1204):  at dalvik.system.NativeStart.main(Native Method) 
07-29 03:08:49.447: E/AndroidRuntime(1204): Caused by: java.lang.NullPointerException 
07-29 03:08:49.447: E/AndroidRuntime(1204):  at app.bayer.notificationbattery.BatteryService$1.onReceive(BatteryService.java:25) 
07-29 03:08:49.447: E/AndroidRuntime(1204):  at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:755) 
07-29 03:08:49.447: E/AndroidRuntime(1204):  ... 9 more 

我在做我的服務或廣播接收器錯?

編輯: 我放置在MainActivity進行通知的方法。

public void buildNotification(Context context){ 
     NotificationManager notificationManager 
     = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); 
     Notification.Builder builder = new Notification.Builder(context); 

     Intent intent = new Intent(context, MainActivity.class); 
     PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); 

     builder 
      .setSmallIcon(R.drawable.boostninja) 
      .setContentTitle("Berocca Performance") 
      .setContentText("Locate our store to charge your phone") 
      .setTicker("Berocca Performance") 
      .setLights(0xFFFF0000, 500, 500) //setLights (int argb, int onMs, int offMs) 
      .setContentIntent(pendingIntent) 
      .setAutoCancel(true); 

     Notification notification = builder.build(); 
     notificationManager.notify(R.drawable.ic_launcher, notification); 
} 
+0

什麼是第25行? – Ahmad

+0

啊,我剛剛意識到我在這條線上犯了一個錯誤 我在MainActivity中調用了我的方法,它是buildNotification(Context context),我在BatteryService中用mainactivity.buildnotification(mainactivity)將其稱爲MainActivity主要活動。我如何糾正它? – thhVictor

+0

您不應該創建活動對象才能訪問其方法。創建一個Utils類,從你的Activity中取出代碼並將它放在那裏。 – Ahmad

回答

1

buildNotification方法放入一個新類中。比方說,你稱之爲'Utils'。

public class Utils { 

public void buildNotification(Context context){...} 

} 

現在這樣稱呼它從服務,並從原始的活動,你從它提取:

Utils utils = new Utils(); 
utils.buildNotification(<context argument goes in here>); 

現在你已經重構它的活動,並沒有創建一個對象的活動,以訪問它。

+0

謝謝它的作品:) – thhVictor

相關問題