我有一个带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" />
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也将正确显示.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。