2016-04-29 6 views
0

Ich verwende den folgenden Code, um eine HTTP-Anforderung zu senden, wenn das Gerät bewegt wird. Es funktioniert gut, wenn ich PARTIAL_WAKE_LOCK zu SCREEN_DIM_WAKE_LOCK ändere und es den Bildschirm eingeschaltet hält und App funktioniert. Wenn Sie jedoch PARTIAL_WAKE_LOCK verwenden, sperrt das Gerät den Bildschirm und die App funktioniert nicht mehr. Ich brauche den Bildschirm ausgeschaltet, damit ich den Akkuverbrauch sparen kann, aber der Wechsel zu PARTIAL_WAKE_LOCK scheint nicht zu funktionieren.Bildschirm in Android mit PARTIAL_WAKE_LOCK ausschalten

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(); 
    } 
} 

Dies ist das Manifest:

<?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> 

Antwort

1

Vergeuden Sie nicht Ihre Zeit. PARTIAL_WAKE_LOCK verhindert nicht, dass Android in den Schlafmodus eintritt, wie Sie here lesen können, und dadurch wird die Sensoraktivität ausgesetzt oder zumindest verzögert. In meinen Tests habe ich gesehen, dass das Verhalten nur dann zu erwarten ist, wenn das Gerät an die Stromversorgung angeschlossen ist.

+0

Ich denke schon. Obwohl, als ich einen Sensor an das Telefon angeschlossen habe, hat es die ganze Zeit gut funktioniert, aber es ist an einem Tag besser. – Amir

Verwandte Themen