Android服务续航时间延续 [英] android services life time continuation

查看:93
本文介绍了Android服务续航时间延续的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在mainactivity
我有广播接收器,悬而未决的意图,并报警经理。它触发按选定的时间(System.currentTimeMillis的()+ smstimeinmilliseconds)。

In mainactivity I have Broadcast Receiver, pending intent, and alarm manager. It triggers as per selected time (System.currentTimeMillis() + smstimeinmilliseconds).

Intent intent = new Intent(this, DBBroadcastReceiver.class);
intent.putExtra("message", message);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, id, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + smstimeinmilliseconds, pendingIntent);

在选择的时候,这个悬而未决的意图触发广播接收机。

On selected time, this pending intent triggers broadcast receiver.

public class DBBroadcastReceiver extends BroadcastReceiver 

@Override
public void onReceive(Context context, Intent intent)
{
message = intent.getStringExtra("message");

}

我可以设置报警经理的活动信息,并设定时间。
每一个事情的作品完美无瑕。我可以激活和关闭这个。但是,如果我设置在今后一段时间一些管理人员报警,并重新启动我的手机。所有的报警经理摧毁.....

I can set message in activity and set time in alarm manager. Every thing works flawless. I can activate and deactivate this. But if i set few alarm mangers in future time and reboot my mobile. all alarm manager destroy .....

请告诉我,有步骤,顺序如何处理活动,广播接收器,而且我需要的服务,如果是的话我如何使用它。

Kindly tell me in steps and sequence what to do with activity , broadcast receiver and do i need service , if yes then how can i use it.

推荐答案

您需要一个BoradcastReceiver被叫做开机。

You need a BoradcastReceiver to be called on boot up.

然后,你需要在你的清单:

Then you need in your manifest :

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

<receiver
    android:name=".broadcasts.YourBroadcastReceiverName">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

和这个广播接收器需要重新安排所有的报警。
是这样的:

And this broadcast receiver needs to schedule again all the alarms. Something like :

public class YourBroadcastReceiverName extends BroadcastReceiver {

    private AlarmManagerFacade alarmManager;

    @Override
    public void onReceive(Context context, Intent intent) {

        // Retreive data related to alarms
        Cursor cursor = context.getContentResolver().query(Alarm.CONTENT_URI, null,
                Alarm.COLUMN_ACTIVE + " = ? ",
                new String[] { String.valueOf(1) }, "");

        if (cursor.moveToFirst()) {

            // Schedule all the active alarms.
            alarmManager = new AlarmManagerFacade(context);
            do {
                // TODO : Schedule alarm according to data in cursor.
            } while (cursor.moveToNext());
        }
        cursor.close();
    }
}

(这code是从我的应用程序之一到来,一些对象不可用在Android SDK)

(This code is coming from one of my app, some of the objects are not available in the Android SDK)

在为了能够重新安排所有报警,你需要的让他们保存的某个地方。

In order to be able to re schedule all the alarms, you need to have them stored somewhere.

您可以编写自己的的ContentProvider 为例。

You can write your own ContentProvider for example.


  • 它的工作原理以及与其他多亏了的CursorAdapter 部件Android组件

  • 这不是最简单的解决方案,但它去,如果你想跟着Android指南的方式。

  • It works well with other android components thanks to the CursorAdapter widget.
  • It is not the easiest solution but it's the way to go if you want to follow android guidelines.

有可能是其他简单的替代来存储您的警报,如共享preferences

There may be other simpler alternative to store your alarms, like SharedPreferences.


  • 这很容易使用。

  • 但是你需要破解各地多个报警存储在一个友好的方式。

最后一个选择是,你可以创建一个包含该信息的对象,序列化,并将其存储作为SD卡的文件。

One last alternative is that you can create an object containing the information, serialize it and store it as a file on the SD Card.


  • 这是丑陋和不灵活。

  • 但它并不难实现...

如果你想仔细看看每个存储选项,你可以在这里文档阅读它:的 http://developer.android.com/guide/topics/data/data-storage.html

If you want to have a closer look to each storage options, you can read about it in the docs here : http://developer.android.com/guide/topics/data/data-storage.html

我希望这一切帮助你。 :)

I hope all this help you. :)

这篇关于Android服务续航时间延续的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆