首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >对话框在主屏幕上显示30秒后不显示

对话框在主屏幕上显示30秒后不显示
EN

Stack Overflow用户
提问于 2020-07-19 23:40:08
回答 2查看 33关注 0票数 0

我想做一个App,30秒后会在主屏幕上显示一个对话框。这个想法是,你点击应用程序中的启用警报对话框并关闭应用程序,然后转到主屏幕。但是,当我单击该按钮时,对话框不会出现。主类有问题吗?我尝试通过这个链接实现代码:http://www.feelzdroid.com/2014/11/how-to-display-custom-dialog-on-android-home-screen.html

下面是我的mainActivity类的代码:

代码语言:javascript
运行
复制
 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用于构建对话框的代码:

代码语言:javascript
运行
复制
 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的代码:

代码语言:javascript
运行
复制
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);
    }
}
EN

回答 2

Stack Overflow用户

发布于 2020-07-20 01:17:44

您显然遵循了您提供的链接中的所有内容,但忘记了在清单文件中添加接收方标记,这是当您离开应用程序时android忽略您的接收方的位置。

代码语言:javascript
运行
复制
<receiver android:name="com.example.homescreendialog.AlertDialogReceiver"/>  
票数 0
EN

Stack Overflow用户

发布于 2020-07-20 01:51:49

好的,我试着让对话框在10秒后出现。不幸的是,它并没有起作用。我试图让应用程序打开,但在这10秒后,应用程序崩溃了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62982370

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档