提醒
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,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 – 是否可以从对话框中调用onReceive方法?全部内容,希望文章能够帮你解决android – 是否可以从对话框中调用onReceive方法?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)