如何保持Android手机的CPU总是醒了吗? [英] How to Keep the CPU of android phone always awake?

查看:199
本文介绍了如何保持Android手机的CPU总是醒了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直停留在好几天。我需要执行一个功能的每一分钟。此功能是使一个POST调用从App到服务器并且被转印每分钟用户的位置。位置的坐标被传输为几个小时,然而,几个小时后位置的传送坐标到服务器停止自身。我利用WakefulBroadcastReceiver和IntentService,以确保CPU的保持清醒。我还利用警报,以确保此功能已被执行的每一分钟。

I have been stuck on it for days. I need to execute a function every minute. This function is making a POST call from the App to the Server and is transferring the location of the user per minute. The location coordinates are transferred for few hours, however, after few hours the transfer of location coordinates to the Server stops on its own. I am making use of WakefulBroadcastReceiver and IntentService to make sure that the CPU stays awake. I am also making use of Alarm to make sure that the function is executed every minute.

这是我的WakefulBroadcastReceiver是这样的:

This is how my WakefulBroadcastReceiver looks like:

public class SimpleWakefulReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    // This is the Intent to deliver to our service.
    Intent service = new Intent(context, SimpleWakefulService.class);

    // Start the service, keeping the device awake while it is launching.
    Log.i("SimpleWakefulReceiver", "Starting service @ " +        
    SystemClock.elapsedRealtime());
    startWakefulService(context, service);
  }
}

这是我的IntentService是这样的:

This is what my IntentService looks like:

public class SimpleWakefulService extends IntentService {
public MainActivity obj;
public Alarm alarm; 
public SimpleWakefulService() {
    super("SimpleWakefulService");
}

@Override
protected void onHandleIntent(Intent intent) {

    boolean flag = true;
        while(flag){
        Log.i("SimpleWakefulService", "Running service ");

        alarm = new Alarm();
        alarm.SetAlarm(SimpleWakefulService.this);
        Log.i("Called alarm.SetAlarm","Called alarm.SetAlarm");

    }
    SimpleWakefulReceiver.completeWakefulIntent(intent);
   // wl.release();

  }
 }

这是我的报警类别如下:

This is how my Alarm class looks like:

public class Alarm extends BroadcastReceiver 
{    
public MainActivity obj;
@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, "");
    wl.acquire();
    obj = new MMMainActivity();
    obj.UpdateData();

    Toast.makeText(context, "Alarm !!!!!!!!!!", Toast.LENGTH_LONG).show(); 
    wl.release();
}

public void SetAlarm(Context context)
{
    Log.i("Inside SetAlarm","Inside SetAlarm");
    AlarmManager am =( AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    Intent i = new Intent(context, Alarm.class);
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
    am.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 60 * 1, pi); // Millisec * Second * Minute


}

public void CancelAlarm(Context context)
{
    Intent intent = new Intent(context, Alarm.class);
    PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarmManager.cancel(sender);
    }
 }

这是什么样的功能(命名为UpdataData()),需要执行的每一分钟,看起来像:

This is what the function(named as UpdataData()), that needs to be executed every minute looks like:

public void UpdateData() {
    final boolean flag = true;
    Log.i("Inside Update Data", "Inside Update Data");

    Thread thread = new Thread(new Runnable() {
       @Override
        public void run() {

            for(int i=0; i<1; i++){
                try {

                    // Your code goes here
                    if (locationclient != null
                            && locationclient.isConnected()) {
                        loc = locationclient.getLastLocation();
                        lat = loc.getLatitude();
                        lng = loc.getLongitude();

                    }
                try { // Updating the latest location in the UI, just
                            // for convinience.
                        HttpClient httpClient = new DefaultHttpClient();
                        // Creating HTTP Post
                        HttpPost httpPost = new HttpPost(
                                "http://www.mywebsite.com/update");
                        List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(
                                3);
                        // Building post parameters//
                        // key and value pair

                        Log.i("token", "token" + token);
                        Log.i("Latitude for III POST Call",
                                "Latitude for III POST Call" + lat);
                        Log.i("Longitude for III POST Call",
                                "Longitude for III POST Call" + lng);


                        nameValuePair.add(new BasicNameValuePair("token",
                                token));
                        nameValuePair.add(new BasicNameValuePair("lat",
                                Double.toString(lat)));
                        nameValuePair.add(new BasicNameValuePair("lng",
                                Double.toString(lng)));
                        Log.i("Made UpdateData Post Call",
                                "Made UpdateData Post Call");
                        // Url Encoding the POST parameters
                        try {
                            httpPost.setEntity(new UrlEncodedFormEntity(
                                    nameValuePair));
                        } catch (UnsupportedEncodingException e) {
                            // writing error to Log
                            e.printStackTrace();
                        }

                        // Making HTTP Request
                        try {
                            HttpResponse response = httpClient
                                    .execute(httpPost);

                            // writing response to log
                            Log.d("Http Response:", response.toString());
                            // Your code goes here
                        } catch (IOException e) {
                            // writing exception to log
                            e.printStackTrace();

                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }// loop closed
           }
      });
    thread.start();
    }// UpdateData closed

为什么即使使用所有这些之后,所述的UpdateData()不能为无限时间执行时,每60秒?任何帮助在这方面将是非常美联社preciated。还要注意的是的updateData()被执行每60秒为几个小时,但在一段时间后,其停止执行其自身,因此位置的传送坐标,从该应用服务器

Why is it that even after using all of these, the UpdateData() cannot be executed for an infinite time, every 60 seconds? Any help in this regard would be highly appreciated. Also note that the UpdateData() is executed every 60 seconds for few hours, but, after some time, its execution stops on its own and so the transfer of location coordinates, from the App to the Server.

编辑:我编辑了SimpleWakefulService和所建议的答案的一个补充所需的更改。事情行之有效的几个小时,然而,后几个小时,该服务会自动死亡。我相信,这些服务的目的不是被杀死。任何人都可以请解释一下吗? SimpleWakefulService:

I have edited the SimpleWakefulService and added the required changes as suggested by one of the answer. Things worked well for few hours, however, post few hours, the service was killed automatically. I believe the services are not meant to be killed. Could anyone please explain this? SimpleWakefulService:

public class SimpleWakefulService extends IntentService implements 
OnClickListener,ConnectionCallbacks, OnConnectionFailedListener, 
LocationListener,GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener {


public static Location loc;
public static LocationClient locationclient;
public static double lat = 10, lng = 10;
public static String token; 

public Alarm alarm; 
public SimpleWakefulService() {
    super("SimpleWakefulService");

}


@Override
protected void onHandleIntent(Intent intent) {


    //Getting the value of token from another Class
    token = ConfirmToken.uvalue;
    Log.i("WakefulServiceutoken", "WakefulServicutoken" + token);

    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
    wl.acquire();

        final boolean flag = true;

        while (flag) {
                    try {

                        // Your code goes here
                        if (locationclient != null
                                && locationclient.isConnected()) {
                            loc = locationclient.getLastLocation();
                            lat = loc.getLatitude();
                            lng = loc.getLongitude();

                        }

                      try { // Updating the latest location in the UI, just
                                // for convinience.
                            HttpClient httpClient = new DefaultHttpClient();
                            // Creating HTTP Post
                            HttpPost httpPost = new HttpPost(
                                    "http://www.mywebsite.com/update");
                            List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(
                                    3);
                            // Building post parameters//
                            // key and value pair
                            Log.i("token", "token" + token);
                            Log.i("Latitude for III POST Call",
                                    "Latitude for III POST Call" + lat);
                            Log.i("Longitude for III POST Call",
                                    "Longitude for III POST Call" + lng);


                            nameValuePair.add(new BasicNameValuePair("token",
                                    token));
                            nameValuePair.add(new BasicNameValuePair("lat",
                                    Double.toString(lat)));
                            nameValuePair.add(new BasicNameValuePair("lng",
                                    Double.toString(lng)));
                                                            // Url Encoding the POST parameters
                            try {
                                httpPost.setEntity(new UrlEncodedFormEntity(
                                        nameValuePair));
                            } catch (UnsupportedEncodingException e) {
                                // writing error to Log
                                e.printStackTrace();
                            }

                            // Making HTTP Request
                            try {
                                HttpResponse response = httpClient
                                        .execute(httpPost);

                                // writing response to log
                                Log.d("Http Response:", response.toString());
                                // Your code goes here
                            } catch (IOException e) {
                                // writing exception to log
                                e.printStackTrace();

                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    try {
                        Thread.sleep(10000);
                    } catch (Exception e) {

                    }

                }// While closed


    //SimpleWakefulReceiver.completeWakefulIntent(intent);
    //wl.release();

}

@Override
public void onDisconnected() {
    // TODO Auto-generated method stub

}

@Override
public void onLocationChanged(Location arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderDisabled(String arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
    // TODO Auto-generated method stub

}

@Override
public void onConnectionFailed(ConnectionResult result) {
    // TODO Auto-generated method stub

}

@Override
public void onConnected(Bundle arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onConnectionSuspended(int arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

}

}

推荐答案

首先,你SimpleWakefulService看起来不是很wakefull,应该aqurie WakeLock,但它没有。例如,这种服务的看这里:

First of all, your SimpleWakefulService does not look very wakefull, it should aqurie WakeLock but it doesnt. For example of such service look here:

<一个href="https://github.com/commonsguy/cwac-wakeful/blob/master/wakeful/src/com/commonsware/cwac/wakeful/WakefulIntentService.java" rel="nofollow">https://github.com/commonsguy/cwac-wakeful/blob/master/wakeful/src/com/commonsware/cwac/wakeful/WakefulIntentService.java

接下来的事情,就是更新功能。这就是所谓的在PARTIAL_WAKE_LOCK是获得性的背景下,但在它的线程异步执行,这也意味着wakelock被释放之后。这是什么造成的问题。

Next thing, is update function. It is called in the context where PARTIAL_WAKE_LOCK is aquired, but the thread within it is executed asynchronously, that means also after wakelock is released. And that is whats causing problems.

解决方案是将自更新功能的线程code(线程不需要),以WakefulIntentService在上面的链接。

Solution is to move your thread code from update function (thread is not needed) to WakefulIntentService as in above link.

这篇关于如何保持Android手机的CPU总是醒了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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