gpt4 book ai didi

android - 关闭应用程序后显示对话框

转载 作者:搜寻专家 更新时间:2023-11-01 08:00:29 26 4
gpt4 key购买 nike

我想在关闭应用程序后显示一个对话框。 (例如,10 秒后。)我用过闹钟:

主要 Activity :

@Override
protected void onStop() {
// TODO Auto-generated method stub
setupAlarm(10);
super.onStop();
}


private void setupAlarm(int seconds) {
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent = new Intent(getBaseContext(), OnAlarmReceive.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
MainActivity.this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);

// Getting current time and add the seconds in it
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, seconds);

alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);

}

我的广播接收器:

public class OnAlarmReceive extends BroadcastReceiver {
Context con = null ;


private final Handler handler = new Handler() {

@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
final Dialog dialog = new Dialog(con);
dialog.setContentView(R.layout.custom_dialog_update);
dialog.setTitle("Title...");

// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Android custom dialog example!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_launcher);

Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});

dialog.show();


super.handleMessage(msg);
}

};
@Override
public void onReceive(Context context, Intent intent) {

// Start the MainActivity
// Intent i = new Intent(context, MainActivity.class);
// i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// context.startActivity(i);
// Toast.makeText(context, "Please Updete Your Clip2iNi", Toast.LENGTH_LONG).show();

con = context ;
handler.sendEmptyMessage(0);
}



}

错误:

02-24 12:38:53.796: E/AndroidRuntime(14711): FATAL EXCEPTION: main 02-24 12:38:53.796: E/AndroidRuntime(14711): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 02-24 12:38:53.796: E/AndroidRuntime(14711): at android.view.ViewRootImpl.setView(ViewRootImpl.java:757) 02-24 12:38:53.796: E/AndroidRuntime(14711): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:265) 02-24 12:38:53.796: E/AndroidRuntime(14711): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69) 02-24 12:38:53.796: E/AndroidRuntime(14711): at android.app.Dialog.show(Dialog.java:282) 02-24 12:38:53.796: E/AndroidRuntime(14711): at com.example.ex56.OnAlarmReceive$1.handleMessage(OnAlarmReceive.java:43) 02-24 12:38:53.796: E/AndroidRuntime(14711): at android.os.Handler.dispatchMessage(Handler.java:99) 02-24 12:38:53.796: E/AndroidRuntime(14711): at android.os.Looper.loop(Looper.java:137) 02-24 12:38:53.796: E/AndroidRuntime(14711): at android.app.ActivityThread.main(ActivityThread.java:5328) 02-24 12:38:53.796: E/AndroidRuntime(14711): at java.lang.reflect.Method.invokeNative(Native Method) 02-24 12:38:53.796: E/AndroidRuntime(14711): at java.lang.reflect.Method.invoke(Method.java:511) 02-24 12:38:53.796: E/AndroidRuntime(14711): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 02-24 12:38:53.796: E/AndroidRuntime(14711): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 02-24 12:38:53.796: E/AndroidRuntime(14711): at dalvik.system.NativeStart.main(Native Method)

没有 Dialog,一切都是正确的。

最佳答案

final Dialog dialog = new Dialog(con)

在这一行 connull 尝试传递 Activity 引用

final Dialog dialog = new Dialog(YourActivity.this)

关于android - 关闭应用程序后显示对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21983505/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com