android – 是否可以从对话框中调用onReceive方法?

我有一个带editText和保存按钮的自定义对话框.单击按钮时,我希望它调用MyReceiver.但是MyReceiver中的日志和Toast永远不会显示出来.

提醒

  final AlertDialog.Builder builder = new AlertDialog.Builder(this);
                LayoutInflater inflater = LayoutInflater.from(this);
                View promptView = getLayoutInflater().inflate(R.layout.dialog_with_edittext, null);
                Button save = (Button) promptView.findViewById(R.id.okBtn);
                final EditText task = (EditText) promptView.findViewById(R.id.task);
                time = (EditText) promptView.findViewById(R.id.time);
                date = (EditText) promptView.findViewById(R.id.date);
                final AlertDialog alert = builder.create();
                date.setonClickListener(this);

                save.setonClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        String addTask= task.getText().toString();
                        String time1= time.getText().toString();
                        String date1= date.getText().toString();
                        if (adapter != null) {
                            adapter.add(addTask,time1,date1);
                            insertTask(addTask, time1, date1);
                            listview.setAdapter(adapter);
                            alert.dismiss();
                            check();
                        }
                        c.set(Calendar.YEAR,year1);
                        c.set(Calendar.MONTH, month1);
                        c.set(Calendar.DAY_OF_MONTH, day1);
                        c.set(Calendar.HOUR_OF_DAY, hour1);
                        c.set(Calendar.MINUTE, min1);
                        c.set(Calendar.SECOND, 0);
                        c.set(Calendar.AM_PM,Calendar.AM);
                        Toast.makeText(getApplicationContext(),year1+""+month1+""+day1+"",Toast.LENGTH_SHORT).show();
                        Toast.makeText(getApplicationContext(),hour1+""+min1+"",Toast.LENGTH_SHORT).show();
                        Intent myIntent = new Intent(ReminderPage.this, MyReceiver.class);
                        pendingIntent = PendingIntent.getbroadcast(ReminderPage.this, 123456789, myIntent,0);
                        AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
                        alarmManager.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingIntent);
                        Toast.makeText(getApplicationContext(),"Alarm",Toast.LENGTH_SHORT).show();
                }
                });
                alert.setView(promptView);
                alert.show();
                return true;

MyReceiver

public class MyReceiver extends broadcastReceiver
{

    @Override
    public void onReceive(Context context, Intent intent)
    {
        Log.i("App", "called receiver method");
        try{
            Toast.makeText(context,"Call Utils1",Toast.LENGTH_SHORT).show();
            Utils1.generateNotification(context);
        }catch(Exception e){
            Toast.makeText(context,"Not Call Utils1",Toast.LENGTH_SHORT).show();
            e.printstacktrace();
        }
    }
}

我还在AndroidMainfest中添加了这个

 <receiver android:name="com.example.MyReceiver"></receiver>

Utils1

public class Utils1 {

        public static notificationmanager mManager;

        @SuppressWarnings("static-access")
        public static void generateNotification(Context context){
            mManager = (notificationmanager) context.getSystemService(context.NOTIFICATION_SERVICE);
            Intent intent1 = new Intent(context,Register.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, intent1, 0);
            Notification.Builder builder = new Notification.Builder(context);
            builder.setAutoCancel(false);
            builder.setTicker("this is ticker text");
            builder.setContentTitle("WhatsApp Notification");
            builder.setContentText("You have a new message");
            builder.setSmallIcon(R.drawable.done);
            builder.setContentIntent(pendingIntent);
            builder.setongoing(true);
            builder.setSubText("This is subtext...");   //API level 16
            builder.setNumber(100);
            builder.build();

            Notification myNotication = builder.getNotification();
            mManager.notify(0, myNotication);
        }
    }

任何帮助将不胜感激.

解决方法:

根据您的问题,以下步骤将为您提供您想要的确切内容.

1.)在AndroidManifest.xml中替换你的接收器

<receiver android:name="com.example.MyReceiver"></receiver>

通过以下方式:

<receiver android:name=".MyReceiver" />

2.)最后在你的代码添加这个按钮监听器:

save.setonClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        // ...
        getApplicationContext().sendbroadcast(
                new Intent(getApplicationContext(), MyReceiver.class));
        // ...
    }
});

就是这样,现在运行你的应用程序.每当您单击保存按钮时,您会注意到MyReceiver类中的onReceive()方法将被正确调用.这意味着你的logcat输出将是

I/App: called receiver method

正如预期的那样,您的Toast消息Call Utils1也将正确显示.

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...