3

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.

Reminder

  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();
        }
    }
}

I also added this in my 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);
        }
    }

Any help would be greatly appreciated.

AI.
  • 934
  • 2
  • 14
  • 30
  • It is possible but you need to make a custom Dialog. – Razvan Cristian Lung Oct 06 '16 at 20:16
  • When you click the button, you are creating an alarm. When that alarm goes off, your `BroadcastReceiver` will be called. Have you checked that the time you are setting the alarm for is correct? Please add some logging to your code and check that `c.getTimeInMillis()` actually contains the value that you think it should. – David Wasser Oct 10 '16 at 18:21

4 Answers4

4

According to your question, the following steps will give you exactly what you want.

1.) In your AndroidManifest.xml replace your receiver

<receiver android:name="com.example.MyReceiver"></receiver>

by the following:

<receiver android:name=".MyReceiver" />

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));
        // ...
    }
});

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

as expected and your Toast message Call Utils1 will also be displayed correctly.

reflective_mind
  • 1,475
  • 3
  • 15
  • 28
  • I use your code and it works. But do you have any idea why the notifications always show without following the time set ? – AI. Oct 10 '16 at 15:32
  • push notifications – AI. Oct 10 '16 at 15:41
  • 1
    The question stated in your title has already been answered and you can accept my answer when it solved this problem. Please open a new question about your notification issue and provide enough detail. It makes no sense to discuss a new question or problem here in the comments section. – reflective_mind Oct 10 '16 at 17:00
1

It is possible but you need to make a custom Dialog. Custom like a new Class that extends DialogFragment. There you create a instance of you receiver and register for it like this:

@Override
protected void onResume() {
 super.onResume();
 getActivity.registerReceiver(mReceiver, mIntentFilter);
}

@Override
protected void onPause() {
 if(mReceiver != null) {
   getActivity.unregisterReceiver(mReceiver);
   mReceiver = null;
   }
 super.onPause();
}
Razvan Cristian Lung
  • 5,661
  • 5
  • 25
  • 49
1

Sending a class-based intent to a broadcast receiver doesn't work. Intents with a class in them are for launching activities, not broadcast messages.

To send a message to a broadcast receiver, you need to use an intent with an action string, and register the string with an intent filter in your manifest.

Jules
  • 14,841
  • 9
  • 83
  • 130
1

If you've already got the data.You can just use it like this:

MyReceiver receiver = new MyReceiver();
receiver.onReceive(context,intent);

Call onReceive() by your code instead of by system.

Qian Sijianhao
  • 564
  • 7
  • 19