【发布时间】:2014-07-20 15:53:49
【问题描述】:
这是我的服务:
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();
}}
我的服务是运动提醒,当特殊时间到来时,我的服务会发送通知。
这很好用,但会消耗大量电池。
在设置>设备中的电池中,我的应用程序排在第一位。
我怎样才能解决这个问题?
我还想在发送通知时打开屏幕。我该怎么做?
【问题讨论】:
-
“我想在发送通知时打开屏幕” 你不能。无论如何,这会消耗更多的电池。另请注意,您在 for 循环的每次传递中都在振动并播放声音……这就是它消耗大量电池的原因。也请使用
isEmpty而不是equals("")。
标签: android service battery wakelock