我想做一个App,30秒后会在主屏幕上显示一个对话框。这个想法是,你点击应用程序中的启用警报对话框并关闭应用程序,然后转到主屏幕。但是,当我单击该按钮时,对话框不会出现。主类有问题吗?我尝试通过这个链接实现代码:http://www.feelzdroid.com/2014/11/how-to-display-custom-dialog-on-android-home-screen.html
下面是我的mainActivity类的代码:
package com.example.homescreendialog;
import androidx.appcompat.app.AppCompatActivity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button mAlertButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAlertButton = (Button)findViewById(R.id.ID_Alert_dialog);
mAlertButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
//Alarm manager
long timestamp = 30000;
Intent intent = new Intent(getApplicationContext(), AlertDialogReceiver.class);
PendingIntent mAlarmSender = PendingIntent.getBroadcast(getApplicationContext(),
167, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager amgr = (AlarmManager) getApplicationContext()
.getSystemService(getApplicationContext().ALARM_SERVICE);
amgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + timestamp,
mAlarmSender);
}
});
}
}以下是dialogClass用于构建对话框的代码:
package com.example.homescreendialog;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
public class AlertDialogClass extends Activity {
AlertDialog.Builder mAlertDlgBuilder;
AlertDialog mAlertDialog;
View mDialogView = null;
Button mOKBtn, mCancelBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
LayoutInflater inflater = getLayoutInflater();
mAlertDlgBuilder = new AlertDialog.Builder(this);
mDialogView = inflater.inflate(R.layout.dialog_layout, null);
mOKBtn = (Button)mDialogView.findViewById(R.id.ID_Ok);
mCancelBtn = (Button)mDialogView.findViewById(R.id.ID_Cancel);
mOKBtn.setOnClickListener(mDialogbuttonClickListener);
mCancelBtn.setOnClickListener(mDialogbuttonClickListener);
mAlertDlgBuilder.setCancelable(false);
mAlertDlgBuilder.setInverseBackgroundForced(true);
mAlertDlgBuilder.setView(mDialogView);
mAlertDialog = mAlertDlgBuilder.create();
mAlertDialog.show();
}
View.OnClickListener mDialogbuttonClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
if(v.getId() == R.id.ID_Ok) {
mAlertDialog.dismiss();
finish();
} else if(v.getId() == R.id.ID_Cancel) {
mAlertDialog.dismiss();
finish();
}
}
};
}这是AlertDialogReceiveClass的代码:
package com.example.homescreendialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class AlertDialogReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent alarmIntent = new Intent("android.intent.action.MAIN");
alarmIntent.setClass(context, AlertDialogClass.class);
alarmIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(alarmIntent);
}
}发布于 2020-07-20 01:51:49
好的,我试着让对话框在10秒后出现。不幸的是,它并没有起作用。我试图让应用程序打开,但在这10秒后,应用程序崩溃了。
https://stackoverflow.com/questions/62982370
复制相似问题