0

I am making a program in which i want to launch an activity, whenever user do double press on power button.

but as when i start my app and tap on Home button and minimize it, After that the MyRecevier is counting the power button counts which is visible in the LOGCAT, but then MyReceiver is unable to launch (make visible on the screen) that minimized activity i.e. the MAINACTIVITY or any othere new Activity.

Log:

02-28 09:58:06.985: E/In on receive(1166): In Method:  ACTION_SCREEN_OFF
02-28 09:58:07.670: E/In on receive(1166): In Method:  ACTION_SCREEN_ON
02-28 09:58:38.170: E/In on receive(1166): In Method:  ACTION_SCREEN_OFF
02-28 09:59:31.355: E/In on receive(1166): In Method:  ACTION_SCREEN_ON

MyReceiver.java:

public class MyReceiver extends BroadcastReceiver 
{
    private static int countPowerOff = 0;

    public MyReceiver ()
    {

    }

    @Override
    public void onReceive(Context context, Intent intent)
    {
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF))
        {    
            Log.e("In on receive", "In Method:  ACTION_SCREEN_OFF");
            countPowerOff++;
        }
        else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON))
        {
            Log.e("In on receive", "In Method:  ACTION_SCREEN_ON");
        }
        else if(intent.getAction().equals(Intent.ACTION_USER_PRESENT))
        {
            Log.e("In on receive", "In Method:  ACTION_USER_PRESENT");
            if (countPowerOff > 2)
            {
                countPowerOff=0;
                Toast.makeText(context, "MAIN ACTIVITY IS BEING CALLED ", Toast.LENGTH_LONG).show();
                Intent i = new Intent(context, MainActivity.class);
                i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);
                context.startActivity(i);
            }
        }
    }
}

MainActivity.java:

public class MainActivity extends Activity {

    private MyReceiver myReceiver;

    @Override
    protected 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);
        filter.addAction(Intent.ACTION_USER_PRESENT);
        myReceiver = new MyReceiver();
        registerReceiver(myReceiver, filter);
    }

    @Override
    protected void onDestroy() 
    {
        if (myReceiver != null)
        {
            unregisterReceiver(myReceiver);
            myReceiver = null;
        }           
        super.onDestroy();
    }

}

Manifest.xml:-

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

    </application>
SunnyLoveU
  • 81
  • 1
  • 9

1 Answers1

-1

By the way if (countPowerOff > 2) means it will have to press 3 times. not two. Give it an >= when the clicks are 2

lantonis
  • 143
  • 14