定期启动服务,耗费大量电池

时间:2015-03-16 11:57:14

标签: android battery-saver

我正在设计一个在后台运行的服务,每小时触发一些小工作。当应用程序运行时,该服务将每10秒触发一次,并在应用程序退出后返回1小时间隔。

我的应用程序不会被打断是非常重要的,所以我使用的是像google文档中推荐的WakefulBroadcastReceiver。

但似乎我的应用程序消耗了大量电池。足以使充电速度明显变慢。电池状态显示系统,屏幕等60%,并没有通知我的应用程序。所以我在想,我的应用程序意外地让手机保持清醒但不使用CPU本身。

由于我想确保不会因为我的应用程序而造成这种情况(并且耗尽其他人的电话),我想问一下我实施服务的方式是否存在明显错误。

主要活动

/*import*/
public class MainActivity extends Activity {

    public static String versionname;
    public static int versioncode;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        AlarmManager alarmMgr;
        PendingIntent alarmIntent;
        alarmMgr = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
        Intent newintent = new Intent(getApplicationContext(), WakefulTickReceiver.class);
        alarmIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, newintent, 0);
        alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                SystemClock.elapsedRealtime() +
                        1 * 1000, alarmIntent);

        pm.setComponentEnabledSetting(receiver,
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP);

        setContentView(R.layout.activity_main);
    }
}

WakefulBroadcastReceiver

/*import*/
public class WakefulTickReceiver extends WakefulBroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // Start the service, keeping the device awake while the service is
        // launching. This is the Intent to deliver to the service.
        Intent service = new Intent(context, TickIntendService.class);
        service.setAction(intent.getAction());
        startWakefulService(context, service);
    }
}

IntentService

/*import*/
public class TickIntendService extends IntentService {

    public static final int NOTIFICATION_ID = 1;
    private NotificationManager mNotificationManager;
    NotificationCompat.Builder builder;

    public void sheduleIntent(){
        AlarmManager alarmMgr;
        PendingIntent alarmIntent;
        alarmMgr = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
        Intent newintent = new Intent(getApplicationContext(), WakefulTickReceiver.class);
        alarmIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, newintent, 0);
        alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                SystemClock.elapsedRealtime() +
                        60 * 60 * 1000, alarmIntent);
    }

    /**
     * The IntentService calls this method from the default worker thread with
     * the intent that started the service. When this method returns, IntentService
     * stops the service, as appropriate.
     */
    @Override
    protected void onHandleIntent(Intent intent) {
        Log.i("IdleHacker","#~# starting service intent on " + intent.toString());



        if (intent.getAction() != null && intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
            Log.i("IdleHacker","starting boot service intent on " + intent.toString());
            sheduleIntent();
            return;
        }

        Bundle extras = intent.getExtras();
        // Do the work that requires your app to keep the CPU running.

        // Normally we would do some work here, like download a file.
        sheduleIntent();
        // Release the wake lock provided by the WakefulBroadcastReceiver.
        WakefulTickReceiver.completeWakefulIntent(intent);
    }

    /**
     * A constructor is required, and must call the super IntentService(String)
     * constructor with a name for the worker thread.
     */
    public TickIntendService() {
        super("TickService");
    }

}

清单

<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" >
    <application>
        <activity android:name=".view.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".service.TickIntendService"></service>
        <receiver android:name=".service.WakefulTickReceiver"
            android:enabled="false">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"></action>
            </intent-filter>
        </receiver>
    </application>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <uses-permission android:name="android.permission.WAKE_LOCK"/>
</manifest>

我试图删除所有不必要的东西,如果缺少某些东西,请写评论!

0 个答案:

没有答案