Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
162 views
in Technique[技术] by (71.8m points)

java - Android - Count Power button clicks and Start Activity

I used the below code, but didnt find the solution.

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) {

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

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

}

and MainActivity.java:

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

    public class MainActivity extends Activity {

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

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
        }
    }

and AndroidManifest.xml:

  <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main" >
        <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>

But i am not getting any Toast Messages on click of Power Button. Please help me on how to get Count of PowerButton clicks and if the clicks equals to 5 then move to another actvity. Please help me to acheive this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try this,

public class MyReceiver extends BroadcastReceiver {
    static int countPowerOff=0;
    private Activity activity=null;
    public MyReceiver (Activity activity)
    {
    this.activity=activity;
    }
    @Override
    public void onReceive(Context context, Intent intent) {

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

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

     if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) 
{
    countPowerOff++;    
} 
else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) 
{
      if(countPowerOff==5)
      {
          Intent i =new Intent(activity,NewActivity.class);
          activity.startActivity(i);
       }
    }

}

And,

public class MainActivity extends Activity {

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
            filter.addAction(Intent.ACTION_SCREEN_OFF);
            MyReceiver mReceiver = new MyReceiver (this);
            registerReceiver(mReceiver, filter);
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
        }
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...