In this code :
1.public class MyReceiver extends BroadcastReceiver 2. public class MainActivity extends Activity
As my application starts I press the 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.
P.S. I want to launch an activity after certain clicks.
MyReceiver.java
public class MyReceiver extends BroadcastReceiver{
final static String TAG = "customTAG";
static int countPowerOff=0;
private Activity activity=null;
public MyReceiver (Activity activity)
{
this.activity=activity;
}
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "Power button is pressed.");
Log.d(TAG, "Power button count is " +countPowerOff);
Toast.makeText(context, "power button clicked", Toast.LENGTH_SHORT).show();
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF))
{
countPowerOff++;
Log.d(TAG, "Action OFF!! Power count increased .");
Log.d(TAG, "Power button count is " +countPowerOff);
}
else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON))
{
Log.d(TAG, "ACTION SCREEN ON");
Log.d(TAG, "Power button count is " +countPowerOff);
if(countPowerOff>2)
{
Log.d(TAG, "Power button count is " +countPowerOff);
Log.d(TAG, "Count is greater than 2 now it should start ");
countPowerOff=0;
Toast.makeText(context, "MAIN ACTIVITY IS BEING CALLED ", Toast.LENGTH_LONG).show();
Intent i = new Intent(activity,MainActivity.class);
//I have tired different flags!!
//below lines are not making any effect unable to popup MainActivity
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
activity.startActivity(i);
context.startService(i);
}
}
}}
MainActivity.java
public class MainActivity extends Activity {
static int a = 0;
MyReceiver mReceiver;
final static String TAG = "customTAG";
@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);
mReceiver = new MyReceiver (this);
registerReceiver(mReceiver, filter);
a++;
Log.d(TAG, "onCreate Event count : " +a);
}
public void onNewIntent()
{
Log.d(TAG, "onNewIntent started !!");
}
public void onStart()
{
super.onStart();
Log.d(TAG, "onStart event Occured !!");
}
public void onResume()
{
super.onResume();
Log.d(TAG, "onResume event occured !!");
}
@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;
}}
AndroidManifest.xml
<application>
..
..
..
..
<receiver android:name="MyReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_SCREEN_ON"></action>
<action android:name="android.intent.action.ACTION_SCREEN_OFF"></action>
</intent-filter>
</receiver>
</application>