4

ユーザーが電源ボタンを押したときにアプリを起動したい。私はこのコードLogをフォローしていますが、トースト が表示されていません。

これが私の完全なコードです。

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

}

}

メニフェストファイル

<?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 file。これを見てください。ありがとう。
4

3 に答える 3

5

まず、他のブロードキャストインテントとは異なり、Intent.ACTION_SCREEN_OFFおよびIntent.ACTION_SCREEN_ONの場合、Androidマニフェストでそれらを宣言することはできません。だからあなたはこのように走り続けるサービスを作る必要があります

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

そしてあなたの受信機は何かになることができます

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

}
于 2013-03-08T11:16:30.743 に答える
2

これは完全なコードで、電源ボタンを押すとすぐにアプリケーションが開きます。私は同じプロジェクトも行っています。ここでは、電源ボタンを押した (電源を入れた) 直後にアプリケーションを開きたいと考えています。

MainActivity.java

 public class MainActivity extends Activity 
   {

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

        startService(new Intent(getApplicationContext(), LockService.class));
    }//EOF Oncreate
    }//EOF Activity

LockService.java

   public class LockService extends Service {

@Override
  public IBinder onBind(Intent intent) {
  return null;
  }
  @Override
  public void onCreate() {
  super.onCreate();
   }

  @Override
  public int onStartCommand(Intent intent, int flags, int startId) 
{
  final IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
  filter.addAction(Intent.ACTION_SCREEN_OFF);
  filter.addAction(Intent.ACTION_USER_PRESENT);
  final BroadcastReceiver mReceiver = new ScreenReceiver();
 registerReceiver(mReceiver, filter);
 return super.onStartCommand(intent, flags, startId);
  }
 public class LocalBinder extends Binder 
{
  LockService getService() {
  return LockService.this;
 }
}//EOF SERVICE

ScreenReceiver.java

public class ScreenReceiver extends BroadcastReceiver {


public static boolean wasScreenOn = true;

public void onReceive(final Context context, final Intent intent) {
Log.e("LOB","onReceive");

if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) 
{
        // do whatever you need to do here
        wasScreenOn = false;
        //Log.e("LOB","wasScreenOn"+wasScreenOn);
        Log.e("Screen ","shutdown now");
 } 
  else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) 
  {
        // and do whatever you need to do here
        wasScreenOn = true;
        Log.e("Screen ","awaked now");

        Intent i = new Intent(context, MainActivity.class);  //MyActivity can be anything which you want to start on bootup...
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);

    }
    else if(intent.getAction().equals(Intent.ACTION_USER_PRESENT))
    {
        Log.e("LOB","userpresent");
      //  Log.e("LOB","wasScreenOn"+wasScreenOn);


    }
}

}//EOF SCREENRECEIVER.JAVA

これはxmlファイルです。コピーして貼り付け、使用しているパッケージ名を変更してください

 <?xml version="1.0" encoding="utf-8"?>

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


<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.userpresent.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>

        <service android:name="com.example.userpresent.LockService" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </service>
</application>

于 2016-11-12T06:34:52.633 に答える