2016-04-29 582 views
0

我正在使用以下代碼在設備移動時發送一些HTTP請求。當我將PARTIAL_WAKE_LOCK更改爲SCREEN_DIM_WAKE_LOCK並使屏幕保持開啓狀態並且應用程序正常工作時,它工作正常。但是,使用PARTIAL_WAKE_LOCK時,設備會鎖定屏幕,並且應用程序完全停止工作。我需要關閉屏幕,以便節省電池使用量,但更改爲PARTIAL_WAKE_LOCK似乎無法達到應有的效果。在Android中使用PARTIAL_WAKE_LOCK關閉屏幕

package com.example.sendsms; 

import android.app.Activity; 
import android.app.DownloadManager; 
import android.content.ContentResolver; 
import android.content.Context; 
import android.database.Cursor; 
import android.hardware.Sensor; 
import android.hardware.SensorEvent; 
import android.hardware.SensorEventListener; 
import android.hardware.SensorManager; 
import android.net.Uri; 
import android.os.Bundle; 
import android.os.PowerManager; 
import android.util.Log; 
import android.view.Menu; 
import android.view.View; 
import android.widget.AutoCompleteTextView; 
import android.widget.FilterQueryProvider; 
import android.widget.SimpleCursorAdapter; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.android.volley.Request; 
import com.android.volley.RequestQueue; 
import com.android.volley.Response; 
import com.android.volley.VolleyError; 
import com.android.volley.toolbox.StringRequest; 
import com.android.volley.toolbox.Volley; 
import com.google.android.gms.appindexing.Action; 
import com.google.android.gms.appindexing.AppIndex; 
import com.google.android.gms.common.api.GoogleApiClient; 


public class SendSMSActivity extends Activity implements SensorEventListener { 
    static String TAG = "SendSMS"; 
    private SensorManager senSensorManager; 
    private Sensor senAccelerometer; 
    private long lastUpdate; 
    private float last_x, last_y, last_z; 
    // Wake Locks 
    protected static PowerManager mPowerManager; 
    protected static PowerManager.WakeLock mWakeLock1; 

    /** 
    * ATTENTION: This was auto-generated to implement the App Indexing API. 
    * See https://g.co/AppIndexing/AndroidStudio for more information. 
    */ 
    private GoogleApiClient client; 


    protected void onResume() { 
     super.onResume(); 
     senSensorManager.registerListener(this, senAccelerometer, SensorManager.SENSOR_DELAY_NORMAL); 
    } 

    protected void onPause() { 
     super.onPause(); 
     senSensorManager.unregisterListener(this); 
    } 

    @Override 
    public void onSensorChanged(SensorEvent sensorEvent) { 
     //To change body of implemented methods use File | Settings | File Templates. 
     Sensor mySensor = sensorEvent.sensor; 

     if (mySensor.getType() == Sensor.TYPE_ACCELEROMETER) { 
      float x = sensorEvent.values[0]; 
      float y = sensorEvent.values[1]; 
      float z = sensorEvent.values[2]; 

      long curTime = System.currentTimeMillis(); 
      // only allow one update every 100ms. 
      if ((curTime - lastUpdate) > 100) { 
       long diffTime = (curTime - lastUpdate); 
       lastUpdate = curTime; 

       float speed = Math.abs(x + y + z - last_x - last_y - last_z)/diffTime * 10000; 
       if (speed > 50) { 
        sendit2(); 
       } 
       last_x = x; 
       last_y = y; 
       last_z = z; 
      } 
     } 
    } 

    @Override 
    public void onAccuracyChanged(Sensor sensor, int accuracy) { 
     // can be safely ignored for this demo 
    } 

    /** 
    * Called when the activity is first created. 
    */ 
    // Accelerometer Wake Lock 
    public static void wakeLock1(boolean up) { 
     if (up) { 
      mWakeLock1 = mPowerManager.newWakeLock(
        PowerManager.PARTIAL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, 
        "WakeLock:Accelerometer" 
      ); 
      mWakeLock1.acquire(); 
     } else { 
      if (mWakeLock1 != null) { 
       if (mWakeLock1.isHeld()) { 
        mWakeLock1.release(); 
       } 
       mWakeLock1 = null; 
      } 
     } 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     // Get an instance of the SensorManager 
     senSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); 
     senAccelerometer = senSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); 
     senSensorManager.registerListener(this, senAccelerometer, SensorManager.SENSOR_DELAY_NORMAL); 

     mPowerManager = (PowerManager)getApplicationContext().getSystemService(Context.POWER_SERVICE); 
     wakeLock1(true); 

     // Important 1: You have to provide a way of making the chosen choice look presentable. 
     // emailAdapter.setStringConversionColumn(1); // 1=DISPLAY_NAME, 2=Email 

     // ATTENTION: This was auto-generated to implement the App Indexing API. 
     // See https://g.co/AppIndexing/AndroidStudio for more information. 
     client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build(); 
    } 

    public void sendit2() { 

     String spreadsheetID = "111111111-1111111111111-FP3OQmeVwV"; 
     String url = "https://script.google.com/macros/s/" + spreadsheetID + "/exec"; 
     // Instantiate the RequestQueue. 
     RequestQueue queue = Volley.newRequestQueue(this); 

     // Request a string response from the provided URL. 
     StringRequest stringRequest = new StringRequest(Request.Method.POST, url, 
       new Response.Listener<String>() { 
        @Override 
        public void onResponse(String response) { 

         Log.d(TAG, response); 
        } 
       }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
      } 
     }); 
     // Add the request to the RequestQueue. 
     queue.add(stringRequest); 
    } 

    @Override 
    public void onStart() { 
     super.onStart(); 

     // ATTENTION: This was auto-generated to implement the App Indexing API. 
     // See https://g.co/AppIndexing/AndroidStudio for more information. 
     client.connect(); 
     Action viewAction = Action.newAction(
       Action.TYPE_VIEW, // TODO: choose an action type. 
       "SendSMS Page", // TODO: Define a title for the content shown. 
       // TODO: If you have web page content that matches this app activity's content, 
       // make sure this auto-generated web page URL is correct. 
       // Otherwise, set the URL to null. 
       Uri.parse("http://host/path"), 
       // TODO: Make sure this auto-generated app URL is correct. 
       Uri.parse("android-app://com.example.sendsms/http/host/path") 
     ); 
     AppIndex.AppIndexApi.start(client, viewAction); 
    } 

    @Override 
    public void onStop() { 
     super.onStop(); 

     // ATTENTION: This was auto-generated to implement the App Indexing API. 
     // See https://g.co/AppIndexing/AndroidStudio for more information. 
     Action viewAction = Action.newAction(
       Action.TYPE_VIEW, // TODO: choose an action type. 
       "SendSMS Page", // TODO: Define a title for the content shown. 
       // TODO: If you have web page content that matches this app activity's content, 
       // make sure this auto-generated web page URL is correct. 
       // Otherwise, set the URL to null. 
       Uri.parse("http://host/path"), 
       // TODO: Make sure this auto-generated app URL is correct. 
       Uri.parse("android-app://com.example.sendsms/http/host/path") 
     ); 
     AppIndex.AppIndexApi.end(client, viewAction); 
     client.disconnect(); 
    } 
} 

這是明顯的:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.sendsms" 
    android:versionCode="1" 
    android:versionName="1.0" > 

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

    <application 
> 
     <activity 
      android:theme="@android:style/Theme.NoTitleBar.Fullscreen" 
      android:name=".SendSMSActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity><!-- ATTENTION: This was auto-generated to add Google Play services to your project for 
    App Indexing. See https://g.co/AppIndexing/AndroidStudio for more information. --> 

     <meta-data 
      android:name="com.google.android.gms.version" 
      android:value="@integer/google_play_services_version" /> 
    </application> 


    <uses-sdk android:minSdkVersion="15"></uses-sdk> 
<uses-permission android:name="android.permission.VIBRATE"></uses-permission> 
</manifest> 

回答

1

不要浪費你的時間。 PARTIAL_WAKE_LOCK不會阻止Android進入休眠模式,因爲您可以讀取here,從而傳感器活動暫停或至少延遲。在我的測試中,我已經看到只有當設備連接到電源時,行爲纔是預期的。

+0

我想是的。儘管當我將傳感器連接到手機時,它的整個時間都工作得很好,但它在一天內消耗了更多的時間。 – Amir