大佬教程收集整理的这篇文章主要介绍了android – 是否可以从对话框中调用onReceive方法?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
提醒
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;@H_425_5@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(); } } }
<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)); // ... } });
就是这样,现在运行你的应用程序.每当您单击保存按钮时,您会注意到R_58_11845@yReceiver类中的onReceive()方法将被正确调用.这意味着你的logcat输出将是
I/App: called receiver method
正如预期的那样,您的Toast消息Call Utils1也将正确显示.
以上是大佬教程为你收集整理的android – 是否可以从对话框中调用onReceive方法?全部内容,希望文章能够帮你解决android – 是否可以从对话框中调用onReceive方法?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。