是否可以从对话框中调用onReceive方法? [英] Is it possible to call onReceive method from a dialog?
问题描述
我有一个自定义对话框,其中包含 editText
和保存按钮
。单击按钮
时,我希望它叫 MyReceiver 。但是 MyReceiver 中的日志和Toast永远不会显示。
I have a custom dialog with editText
and save button
. When button
clicked, I want it call MyReceiver. But the log and Toast in MyReceiver never get displayed.
提醒
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
I also added this in my AndroidMainfest
<receiver android:name="com.example.MyReceiver"></receiver>
实用程序1
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);
}
}
任何帮助将不胜感激。
Any help would be greatly appreciated.
推荐答案
根据您的问题,以下步骤将为您提供所需的确切信息。
According to your question, the following steps will give you exactly what you want.
1。)在您的 AndroidManifest.xml 中,替换您的接收器
1.) In your AndroidManifest.xml replace your receiver
<receiver android:name="com.example.MyReceiver"></receiver>
通过以下方式:
<receiver android:name=".MyReceiver" />
2。)最后,将其添加到按钮侦听器的代码中:
2.) Finally add this in your code for the button listener:
save.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// ...
getApplicationContext().sendBroadcast(
new Intent(getApplicationContext(), MyReceiver.class));
// ...
}
});
就是这样,现在运行您的应用程序。每当您单击您的保存
按钮时,您会注意到类 onReceive()
方法> MyReceiver 将被正确调用。这意味着您的logcat输出将为
That's it, now run your app. Whenever you click your save
button, you will notice that the onReceive()
method in your class MyReceiver
will get called properly. That means your logcat output will be
I/App: called receiver method
与预期的一样,并且您的 Toast
消息致电Utils1
也将正确显示。
as expected and your Toast
message Call Utils1
will also be displayed correctly.
这篇关于是否可以从对话框中调用onReceive方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!