英文:
The AlertDialog does not appear on the screen when the application is killed
问题
警告用户Wi-Fi已打开;我想在屏幕上显示一个弹出窗口。我尝试使用广播和服务来实现。然而,应用程序被终止后不起作用。
我的服务代码:
public class AlertService extends Service {
private static Context context;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
final AlertDialog alertDialog = new AlertDialog.Builder(AlertService.this).create();
alertDialog.setMessage("Nek测试");
alertDialog.setTitle("提醒");
alertDialog.setButton("知道了", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
} else {
alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
}
new CountDownTimer(1000, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
alertDialog.show();
}
}.start();
return flags;
}
}
MainActivity:
private BroadcastReceiver wifiStateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
int wifiStateExtra = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN);
switch (wifiStateExtra) {
case WifiManager.WIFI_STATE_ENABLED:
this.context = context;
startService(new Intent(context, AlertService.class));
break;
case WifiManager.WIFI_STATE_DISABLED:
break;
}
}
};
英文:
Warns the user when wifi is turned on; I want to show a popup on the screen. I tried to do it with broadcast and service. However, it does not work after the application is killed.
My Service Code :
public class AlertService extends Service {
private static Context context;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
final AlertDialog alertDialog = new AlertDialog.Builder(AlertService.this).create();
alertDialog.setMessage("Nek Test");
alertDialog.setTitle("Reminder");
alertDialog.setButton("Got it", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
}else{
alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
}
new CountDownTimer(1000, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
alertDialog.show();
}
}.start();
return flags;
}
MainActivity :
private BroadcastReceiver wifiStateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
int wifiStateExtra = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,WifiManager.WIFI_STATE_UNKNOWN);
switch (wifiStateExtra){
case WifiManager.WIFI_STATE_ENABLED:
this.context = context;
startService(new Intent(context, AlertService.class));
break;
case WifiManager.WIFI_STATE_DISABLED:
break;
}
}
};
答案1
得分: 0
抱歉,在最近的 Android 设备上,如果应用程序已被终止,您无法从后台启动活动。
我可以建议您执行以下操作:
- 检查在终止应用程序后,您的服务是否继续运行。如果没有 - 您需要使其独立于应用程序生命周期。
- 您可以显示一个 Toast 或全屏意图来通知用户。
英文:
Unfortunately you cannot start activity from the background in case app has been killed on recent Android devices.
I can recommend you to do next things:
- check if your service continue running after killing the app. If it doesn't - you need to make it independent from app lifecycle.
- you could show a toast \ full screen intent to notify a user
专注分享java语言的经验与见解,让所有开发者获益!
评论