1
這是我的服務:爲什麼我的服務使用大量電池?
public class reminder extends Service{
WakeLock wakeLock;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock");
wakeLock.acquire();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Timer t=new Timer();
final NotificationManager mgr=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//check array alarm
final SharedPreferences settings = getSharedPreferences("arrayalarm", 0);
final String sport = settings.getString("sport", "");
final String[] sports=sport.split("#");
TimerTask tt=new TimerTask() {
@Override
public void run() {
if(!(sports[0].equals("")))
for(int i=0;i<sports.length;i++)
{
String[] informatins=sports[i].split(":");
if(new Date().getHours()==Integer.parseInt(informatins[0])&&new Date().getMinutes()==Integer.parseInt(informatins[1]))
{
Notification not=new Notification(R.drawable.sport,"ورزش",new Date().getTime());
PendingIntent pn=PendingIntent.getActivity(getApplicationContext(),0,new Intent(getApplicationContext(),MainActivity.class), 0);
not.setLatestEventInfo(getApplicationContext(), "ورزش", "تو الان باید ورزش کنی", pn);
not.flags=Notification.FLAG_AUTO_CANCEL;
not.vibrate=new long[] {1000l,200l,200l,500l};
not.sound=Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.downloaddlazerdompleted);
mgr.notify(1304,not);
}
}
}
};
t.scheduleAtFixedRate(tt, 0, 60000);
return START_STICKY;
}
@Override
public void onDestroy() {
wakeLock.release();
//Toast.makeText(getApplicationContext(), "god bye main human",Toast.LENGTH_SHORT).show();
super.onDestroy();
}}
我的服務是提醒運動,並在特殊的時間來到我的服務發送通知。
這工作得很好,但使用了很多電池。 在設置>電池在設備中,我的應用程序首先在列表中。
我該如何解決這個問題? 我也想在通知發送時打開屏幕。我怎樣才能做到這一點?
*「我想在通知發送時打開屏幕」*您不能。無論如何,這將消耗更多的電池。另外請注意,您每次循環播放音樂時都會發出震動和聲音......這就是爲什麼它會消耗大量電量。也請使用'isEmpty'而不是'equals(「」)'。 – m0skit0