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

时间:2016-10-06 16:33:40

标签: android broadcastreceiver alarmmanager android-broadcastreceiver

我有editTextsave button自定义对话框。点击button后,我希望它调用 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);
        }
    }

非常感谢任何帮助。

4 个答案:

答案 0 :(得分:4)

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

1。) AndroidManifest.xml 中替换receiver

<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));
        // ...
    }
});

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

I/App: called receiver method

符合预期,您的Toast邮件Call Utils1也会正确显示。

答案 1 :(得分:1)

这是可能的,但您需要制作自定义对话框。自定义像一个扩展DialogFragment的新类。在那里你创建了一个接收器实例并注册它:

@Override
protected void onResume() {
 super.onResume();
 getActivity.registerReceiver(mReceiver, mIntentFilter);
}

@Override
protected void onPause() {
 if(mReceiver != null) {
   getActivity.unregisterReceiver(mReceiver);
   mReceiver = null;
   }
 super.onPause();
}

答案 2 :(得分:1)

向广播接收器发送基于类的意图不起作用。其中包含类的意图用于启动活动,而不是广播消息。

要向广播接收器发送消息,您需要使用带有操作字符串的intent,并在清单中使用intent过滤器注册该字符串。

答案 3 :(得分:1)

如果您已经获得了数据。您可以像这样使用它:

MyReceiver receiver = new MyReceiver();
receiver.onReceive(context,intent);

通过代码而不是系统调用onReceive()

相关问题