- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个带有 editText
和 save 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;
我的接收者
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>
实用工具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);
}
}
如有任何帮助,我们将不胜感激。
最佳答案
根据您的问题,以下步骤将为您提供您想要的。
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
按钮时,您会注意到类 MyReceiver
中的 onReceive()
方法将被正确调用。这意味着您的 logcat 输出将是
I/App: called receiver method
正如预期的那样,您的 Toast
消息 Call Utils1
也将正确显示。
关于android - 是否可以从对话框中调用 onReceive 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39901321/
是否可以让通知启动广播接收器? 我试过这段代码,但它不起作用。 通知已创建,但当我点击它时没有任何反应。 注意:当我将 notificationIntent 更改为从 MyBroadcastRecei
下面是我的 Java 代码和 XML 代码。有人可以告诉我为什么我的 onReceieve 方法永远不会被调用吗? Java: public class PopUPSMS extends Activi
onReceive 永远不会接到电话!我已经完成了这个工作,并且做了一些更改,现在我已经为此工作了 5 个小时,并且不知道当我向它发送短信时我的 onReceive 没有被调用! public voi
我正在尝试遵循这个例子: What is the correct way for sharing data among AppWidgetProvider and RemoteViewsService
我在 Activity 中创建了一个接收器,用于在连接互联网时自动调用网络服务。 代码如下 //Create receiver for while network will come auto cal
最近对Android开发很感兴趣, friend 给了我一本《Android应用开发傻瓜书》。在书中,有一个名为 Silent Toggle Mode 的示例应用程序。稍后它会教您为应用程序制作主屏幕
我在 mainfest.xml 中声明了我的 BroadcastReceiver 但从未调用过 onReceive() 方法。怎么了?我不想使用注册/注销方法。 Activity : Intent s
我有以下代码我想检测手机屏幕但是永远不会调用函数 onReceive谁能给我一个解决方案。请帮忙。如果有人有任何其他工作代码,请提供帮助。谢谢 package com.pack; import and
我创建了一个 broadcastreceiver 来 Hook 日历 EVENT_REMINDER 并获取有关事件的信息 public void onCreate(Bundle savedInstan
我读了这篇文章:Broadcast receiver onReceive() getting called multiple times 但我没有找到所需的答案。 我创建了一个小实用函数,它覆盖了 B
onReceive 方法中的线程在完成之前是否符合垃圾回收条件? @Override public void onReceive(final Context context, Intent inten
我正在使用此代码来检测屏幕何时被锁定并调用 toast,每次屏幕被锁定时它都会工作。但是,每当我离开应用程序时,它就会停止工作。它仅在应用程序打开时有效。 public class BatterySa
public class BroadcastTest extends Activity { BroadcastReceiver receiver; /** Called when the activi
我有一个从其他应用程序获取数据的服务。 当我得到日期时,我会向 broadCast 发送消息以刷新 UI。 多次调用onReceive方法,多次显示数据。 这是我的代码: DataService.ja
当我尝试接收的变量是@EnvironmentObject 的@Published 属性时,有什么方法可以防止在 View 最初加载时触发onReceive? 这是 View : struct Cont
我正在使用 Picker 来显示分段控件,并希望知道选择器值何时更改,以便我可以执行非 UI 操作。使用建议的 onReceive() 修饰符(如建议的 here )不起作用,因为每次渲染主体时都会调
我是 Android 初学者,遇到以下问题。我正在尝试从(不同的)应用程序 B 调用应用程序 A 注册的广播接收器。在应用程序 A 的广播接收器中,我实现了以下 onReceive() 方法,用于测试
我正在尝试开发一个短信应用程序。当我选择此应用程序作为默认短信应用程序并且当我收到带有此 SmsReceiver 代码的新短信时,会出现以下问题: 1) 每条新短信我都会收到 2 个 onReceiv
我正在设置一个内部BroadcastReceiver来获取 Activity 转换更新,例如步行、in_Vehicle。但 onReceive 方法永远不会被调用。 onCreate @Overri
我正在编写一个 Android 应用程序,它有一个受权限保护的广播接收器。广播接收器在接收来自经过身份验证的应用程序的请求时扫描硬编码 IP 地址的端口 80。它返回套接字连接尝试的结果。在我的代码中
我是一名优秀的程序员,十分优秀!