如何启动电源按钮preSS应用程序 [英] how to start the app on power button press

查看:158
本文介绍了如何启动电源按钮preSS应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要开始我的应用程序,当用户preSS的电源按钮。我米以下的<一个href="http://stackoverflow.com/questions/8940922/activate-an-application-when-a-power-button-is-clicked">This code 但它没有显示任何登录和烤面包片。

I want to start my app when a user press the power button. I m following This code but its not showing any Log and toast.

这是我的完整的code。

here is my complete code.

MyReceiver.java

import android.content.BroadcastReceiver;
   import android.content.Context;
   import android.content.Intent;
   import android.util.Log;
   import android.widget.Toast;

   public class MyReceiver extends BroadcastReceiver {

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

    // TODO Auto-generated method stub

    Log.v("onReceive", "Power button is pressed.");

    Toast.makeText(context, "power button clicked", Toast.LENGTH_LONG)
            .show();

    // perform what you want here

}

}

menifest文件

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

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.powerbuttontest.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver android:name=".MyReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.SCREEN_OFF" >
            </action>
            <action android:name="android.intent.action.SCREEN_ON" >
            </action>
            <action android:name="android.intent.action.ACTION_POWER_CONNECTED" >
            </action>
            <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" >
            </action>
            <action android:name="android.intent.action.ACTION_SHUTDOWN" >
            </action>
        </intent-filter>
    </receiver>
</application>
</manifest>

MainActivity.java

package com.example.powerbuttontest;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

  • 我想我犯在我的 menifest文件中的错误。请对此看看。谢谢。
    • I think i m committing a mistake in my menifest file. please have a look on this. thanks.
    • 推荐答案

      首先,与其他广泛的铸造意图,为Intent.ACTION_SCREEN_OFF和Intent.ACTION_SCREEN_ON不能在你的Andr​​oid清单申报他们!所以,你需要做,这将继续这样运行的服务

      First, unlike other broad casted intents, for Intent.ACTION_SCREEN_OFF and Intent.ACTION_SCREEN_ON you CANNOT declare them in your Android Manifest! so You need to make a service which will keep on running like this

      public static class UpdateService extends Service {
      
              @Override
              public void onCreate() {
                  super.onCreate();
                  // register receiver that handles screen on and screen off logic
                  IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
                  filter.addAction(Intent.ACTION_SCREEN_OFF);
                  BroadcastReceiver mReceiver = new Receiver();
                  registerReceiver(mReceiver, filter);
              }
      
              @Override
              public void onStart(Intent intent, int startId) {
                  boolean screenOn = intent.getBooleanExtra("screen_state", false);
                  if (!screenOn) {
                      // your code
                  } else {
                      // your code
                  }
              }
      }
      

      和你的接收器可以是一些

      and your receiver can be something

      public class Receiver extends BroadcastReceiver {
      
          private boolean screenOff;
      
          @Override
          public void onReceive(Context context, Intent intent) {
              if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
                  screenOff = true;
              } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
                  screenOff = false;
              }
              Intent i = new Intent(context, UpdateService.class);
              i.putExtra("screen_state", screenOff);
              context.startService(i);
          }
      
      }
      

      这篇关于如何启动电源按钮preSS应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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