所以,我会解释我的情况。 我有一个服务,它从电源按钮捕获事件。在服务中我有一个广播接收器,它接受Intent.ACTION_SCREEN_OFF和Intent.ACTION_SCREEN_ON。
// BroadcastReceiver for handling ACTION_SCREEN_OFF.
public BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Check action just to be on the safe side.
if ((intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) ||
(intent.getAction().equals(Intent.ACTION_SCREEN_ON)) ) {
if((System.currentTimeMillis() - delta) < DOUBLE_CLICK_INTERVAL)
{
count++;
Log.e("AAAAAAAA", "count= "+count);
}else{
count = 0;
}
if(count == 4){
Log.e("AAAAAAAA", "EVENT TRIGGERED!!!!!");
Intent alarmIntent = new Intent("com.myaction.action.EVENT_TRIGGERED");
sendBroadcast(alarmIntent);
}
delta = System.currentTimeMillis();
}
}
};
我在onCreate中注册接收器,然后在onDestroy()中注销它。 我有另一个BroadcastReceiver(这是在清单中注册的)捕获我的动作(EVENT_TRIGGERED)。广播接收器启动包装对话的活动。
public class AlarmBroadReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.e("BROADCAST", "----------GOT THE EVENT---------");
Intent newActivityDialog = new Intent(context, NotifyDialog.class);
newActivityDialog .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(newActivityDialog );
}}
广播所调用的活动如下:
public class NotifyDialog extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Log.e("DIALOGACTIVITY", "ON CREATE");
final Window win = getWindow();
win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.e("DIALOGACTIVITY", "ON START");
displayAlert();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.e("DIALOGACTIVITY", "ON RESUME");
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.e("DIALOGACTIVITY", "ON PAUSE");
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.e("DIALOGACTIVITY", "ON DESTROY");
}
private void displayAlert()
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Random question?").setCancelable(
false).setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//do somethin
dialog.cancel();
finish();
}
}).setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
finish();
}
});
AlertDialog alert = builder.create();
alert.show();
}}
当我按下(几次)电源按钮时,我的动作被广播并被启动活动的适当接收器捕获。我的问题是,活动被调用了两次,我没有理由,日志给出了发生了什么的想法:
11-04 16:20:22.339: E/AAAAAAAA(19451): count= 1
11-04 16:20:22.562: E/AAAAAAAA(19451): count= 2
11-04 16:20:22.886: E/AAAAAAAA(19451): count= 3
11-04 16:20:22.983: E/AAAAAAAA(19451): count= 4
11-04 16:20:22.983: E/AAAAAAAA(19451): EVENT TRIGGERED!!!!!
11-04 16:20:23.073: E/AAAAAAAA(19451): count= 5
11-04 16:20:23.077: E/MAMT FESC(19451): ----------GOT SOMETHING---------
11-04 16:20:23.085: E/DIALOGACTIVITY(19451): ON CREATE
11-04 16:20:23.085: E/DIALOGACTIVITY(19451): ON START
11-04 16:20:23.112: E/DIALOGACTIVITY(19451): ON RESUME
11-04 16:20:23.499: E/DIALOGACTIVITY(19451): ON PAUSE
11-04 16:20:23.499: E/DIALOGACTIVITY(19451): ON DESTROY
11-04 16:20:23.554: E/WindowManager(19451): Activity com.notfall.NotifyEmergencyCall has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@482c8228 that was originally added here
11-04 16:20:23.554: E/WindowManager(19451): android.view.WindowLeaked: Activity com.notfall.NotifyEmergencyCall has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@482c8228 that was originally added here
11-04 16:20:23.554: E/WindowManager(19451): at android.view.ViewRoot.<init>(ViewRoot.java:247)
11-04 16:20:23.554: E/WindowManager(19451): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:181)
11-04 16:20:23.554: E/WindowManager(19451): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:124)
11-04 16:20:23.554: E/WindowManager(19451): at android.view.Window$LocalWindowManager.addView(Window.java:424)
11-04 16:20:23.554: E/WindowManager(19451): at android.app.Dialog.show(Dialog.java:241)
11-04 16:20:23.554: E/WindowManager(19451): at com.notfall.NotifyEmergencyCall.displayAlert(NotifyEmergencyCall.java:115)
11-04 16:20:23.554: E/WindowManager(19451): at com.notfall.NotifyEmergencyCall.onStart(NotifyEmergencyCall.java:54)
11-04 16:20:23.554: E/WindowManager(19451): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
11-04 16:20:23.554: E/WindowManager(19451): at android.app.Activity.performStart(Activity.java:3781)
11-04 16:20:23.554: E/WindowManager(19451): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2636)
11-04 16:20:23.554: E/WindowManager(19451): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-04 16:20:23.554: E/WindowManager(19451): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-04 16:20:23.554: E/WindowManager(19451): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-04 16:20:23.554: E/WindowManager(19451): at android.os.Handler.dispatchMessage(Handler.java:99)
11-04 16:20:23.554: E/WindowManager(19451): at android.os.Looper.loop(Looper.java:123)
11-04 16:20:23.554: E/WindowManager(19451): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-04 16:20:23.554: E/WindowManager(19451): at java.lang.reflect.Method.invokeNative(Native Method)
11-04 16:20:23.554: E/WindowManager(19451): at java.lang.reflect.Method.invoke(Method.java:521)
11-04 16:20:23.554: E/WindowManager(19451): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
11-04 16:20:23.554: E/WindowManager(19451): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11-04 16:20:23.554: E/WindowManager(19451): at dalvik.system.NativeStart.main(Native Method)
11-04 16:20:23.554: E/DIALOGACTIVITY(19451): ON CREATE
11-04 16:20:23.558: E/DIALOGACTIVITY(19451): ON START
11-04 16:20:23.620: E/DIALOGACTIVITY(19451): ON RESUME
11-04 16:20:23.636: E/DIALOGACTIVITY(19451): ON PAUSE
11-04 16:20:23.815: E/DIALOGACTIVITY(19451): ON RESUME
11-04 16:20:24.105: E/AAAAAAAA(19451): count= 1
11-04 16:21:19.589: E/DIALOGACTIVITY(19451): ON PAUSE
11-04 16:21:19.667: E/DIALOGACTIVITY(19451): ON DESTROY
我的猜测是窗口泄漏是由对活动的双重调用引起的。正如您所看到的,广播仅发送和接收一次。对于长邮件很抱歉,但我必须解释整个情况,以便你们了解为什么活动被调用两次。谢谢你们。
丹尼尔
答案 0 :(得分:2)
我不确定您的活动为何开始两次,但您可以通过清除对话框来消除泄漏。使对话框成为Activity的成员变量,如果存在则解除并将其取消onStop。
答案 1 :(得分:1)
除了预期的情况之外,我观察到只有那些活动(onCreate)被调用两次,这些活动正在创建新的Thread或Runnable。 (我相信这是Android中的一个错误。)
解决方案很简单(尽管你可能不喜欢它:p)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
if(savedInstanceState == null){
// everything else that doesn't update UI
}
}
答案 2 :(得分:0)
活动没有开始两次。
因为
====>at android.app.Activity.performStart(Activity.java:3781)
在您创建应用程序时,这只启动一次。