较慢的Andr​​oid在未连接到充电器? [英] Slower android when not connected to charger?

查看:104
本文介绍了较慢的Andr​​oid在未连接到充电器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想开发简单的Andr​​oid应用程序,它使用监控HTTP客户端指定的网址,一旦定义的条件时,应该执行通知操作。

I'm trying to develop simple android application which monitors specified url using http client and once defined condition is met it should perform notification actions.

我有一个活动而启动单独的线程,并通过静态值把引用。 (当活动被重建,我chechking参考NOT NULL来确定的,如果孩子线程已经启动)。在这个子线程我有while循环,从URL获取JSON数据并解析它。

I have one activity which starts separate thread and put reference to it via static value. (When activity is recreated I'm chechking reference for not null to determinate if child thread was already started). In this child thread I have while loop which gets json data from url and parse it.

我注意到奇怪的水煤浆(也许是因为我是Android开发的新手)。当应用程序在前台它的工作原理相当快,当Android设备进入睡眠模式,它不执行请求频繁。 (也许有些能源安全的政策是什么?)。什么是最奇怪的是,一旦我通过USB线连接手机到电脑工程快(甚至当应用程序在后台和手机有黑色的屏幕)。

I've noticed weird behavour (maybe because I'm android dev newbie). Once application is in foreground it works quite fast, when android device goes into sleep mode it doesn't perform requests to often. (maybe some energy safe policy?). What's the most weird is that once I connected phone to computer via usb cable to works fast (even when application is in background and phone has black screen).

有没有来激活基于连接的充电器/ disactivati​​ng应用什么关系? 我无法调试它,因为我一旦连接电缆能正常工作,而我不能调试无需连接到电脑。

Is there any relationship to activating /disactivating applications based on connected charger? I can't debug it because once I connected cable it works fine, and I can't debug without being connected to computer.

推荐答案

这件事可能是手机的推移,当它停止几乎所有的活动,并减慢CPU睡眠模式。它用于节省电池。在 Handler.postDelayed()计时器例如将无法正常工作的正确的(不要求时间)。 有这件事情特别的概念 - 对于需要在睡眠模式下进行的活动,你需要使用 AlarmManager ,看到的调度重复报警

The matter probably is that phone goes to sleep mode when it stops almost all activity and slows down CPU. It is used to save battery. Timers on Handler.postDelayed() for example won't work properly (not called on time). There's special concept for this matter - for activities that needs to be performed in sleep mode, you need to use AlarmManager, see Scheduling Repeating Alarms

这个问题是,你的应用程序需要使用的 AlarmManager 注册,然后它会接受预定的事件时,手机从休眠模式唤醒。您的应用程序需要获得锁的电源管理器执行活动(在你的情况下,它从网络下载JSON),你不希望与睡眠模式,而你执行它们中断。考虑这个例子:

The matter is that your app needs to register with AlarmManager, and then it will receive scheduled events when phone wakes up from sleep mode. Your app needs to get lock with PowerManager to perform activities (in your case it's downloading JSON from network), which you do not want to be interrupted with sleep mode while you're executing them. Consider this example:

public class AlarmManagerBroadcastReceiver extends BroadcastReceiver {

    /**
     * This method is called when we are waking up by AlarmManager
     */
    @Override
    public void onReceive(Context context, Intent intent) {
        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "pulse app");
        //Acquire the lock
        wl.acquire();

        //You can do the processing here.

        //Release the lock
        wl.release();
    }

    /**
     * Register our app with AlarmManager to start receiving intents from AlarmManager
     */
    public static void setAlarm(Context context)
    {
        int interval = 10; // delay in secs
        AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
        PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
        am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval*1000 , pi);
    }

    /**
     * Unregister the app with AlarmManager, call this to stop receiving intents from AlarmManager
     */
    public static void cancelAlarm(Context context)
    {
        Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
        PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarmManager.cancel(sender);
    }

}

这code需求 android.permission.WAKE_LOCK 许可清单文件。

关于另一个职位的 AlarmManager 用法:的Andr​​oid AlarmManager

Another post about AlarmManager usage: Android AlarmManager

和这样的:进入睡眠模式,当应用程序正在运行 prevent移动

And this: prevent mobile from going into sleep mode when app is running

在文章第一个环节说,这是preferable使用同步适配器这样的目的,但我没有用他们自己。

Article at the first link says that it's preferable to use Sync Adapters for such purpose, but I haven't used them myself.

这篇关于较慢的Andr​​oid在未连接到充电器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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