Android BroadCast Reveiver正在计算电源按钮的单击次数,但无法恢复最小化的活动
在此代码中: 1.公共类MyReceiver扩展了BroadcastReceiver 2.公共类MainActivity扩展活动 当我的应用程序启动时,我按下HOME按钮并最小化它!之后,MyReceiver正在计算LOGCAT中可见的电源按钮计数,但MyReceiver无法启动(在屏幕上显示)最小化活动,即Main活动或任何其他新活动 另外,我想在点击后启动一项活动Android BroadCast Reveiver正在计算电源按钮的单击次数,但无法恢复最小化的活动,android,android-intent,android-activity,android-broadcast,Android,Android Intent,Android Activity,Android Broadcast,在此代码中: 1.公共类MyReceiver扩展了BroadcastReceiver 2.公共类MainActivity扩展活动 当我的应用程序启动时,我按下HOME按钮并最小化它!之后,MyReceiver正在计算LOGCAT中可见的电源按钮计数,但MyReceiver无法启动(在屏幕上显示)最小化活动,即Main活动或任何其他新活动 另外,我想在点击后启动一项活动 MyReceiver.java public class MyReceiver extends BroadcastReceive
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);
}
}
}}
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;
}}
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);
}
}
}
}
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();
}
}
MainActivity.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);
}
}
}}
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;
}}
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);
}
}
}
}
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();
}
}
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>
..
..
..
..
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);
}
}
}}
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;
}}
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);
}
}
}
}
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();
}
}
MainActivity.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);
}
}
}}
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;
}}
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);
}
}
}
}
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();
}
}
如果您已在活动中注册接收人,则无需在清单中注册接收人。
此外,最好从操作\u用户\u出现的位置开始活动
,因为这意味着设备在按下电源按钮锁定后已解锁。但是,如果您想从上的操作屏幕启动活动,您也可以这样做,这也会起作用,但只有在用户解锁设备后,活动才会可见。
此代码正在运行。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);
}
}
}}
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;
}}
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);
}
}
}
}
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();
}
}
MainActivity.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);
}
}
}}
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;
}}
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);
}
}
}
}
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();
}
}
如果您已在活动中注册接收人,则无需在清单中注册接收人。
此外,最好从操作\u用户\u出现的位置开始活动
,因为这意味着设备在按下电源按钮锁定后已解锁。但是,如果您想从
上的操作屏幕启动活动,您也可以这样做,这也会起作用,但只有在用户解锁设备后,活动才会可见。
此代码正在运行