Note that there are some explanatory texts on larger screens.

plurals
  1. POInfinitely running broadcast receiver in android
    primarykey
    data
    text
    <p>In my project, I have a database containing reminders. I want to compare the date and time stored on database with current date and time and give an alarm. I've used a Broadcast receiver for this. The problem I'm facing is that the receiver is running infinitely and is not allowing the activity to come on the screen. Also, I've checked if the date and time matches in an if condition and called the receiver only if it matches. It's entering the condition and starting the receiver even if the condition is false. Here's my code. Any help will be greatly appreciated. Thanks!</p> <p>Code for retrieving values from database, comparing values and starting receiver:</p> <pre><code> public class DisplayReminderList extends Activity { ListView lv; Intent intent; public MediaPlayer mMediaPlayer; ArrayAdapter&lt;String&gt; adapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.display_reminder_list); lv = (ListView) findViewById(R.id.when_display_listview_today); adapter = new ArrayAdapter&lt;String&gt;(getApplicationContext(), android.R.layout.simple_list_item_1); SQLiteDatabase database = new SQLiteHelper(getApplicationContext()) .getWritableDatabase(); Cursor c = database.rawQuery("select title from Reminder", null); c.moveToFirst(); while (!c.isAfterLast()) { System.out.println("Data : " + c.getString(0)); adapter.add(c.getString(0)); c.moveToNext(); } lv.setAdapter(adapter); c.close(); Cursor c1 = database.rawQuery( "select title,reminderdate,remindertime from Reminder", null); Calendar cal; String when_reminder_date, when_reminder_time, d, t, when_reminder_title; c1.moveToFirst(); while (!c1.isAfterLast()) { when_reminder_title = c1.getString(0); d = c1.getString(1); t = c1.getString(2); cal = Calendar.getInstance(); when_reminder_date = String.valueOf(cal.get(Calendar.YEAR)) + "-" + String.valueOf(cal.get(Calendar.MONTH)) + "-" + String.valueOf(cal.get(Calendar.DAY_OF_MONTH)); when_reminder_time = String.valueOf(cal.get(Calendar.HOUR_OF_DAY)) + ":" + String.valueOf(cal.get(Calendar.MINUTE)) + ":" + String.valueOf(cal.get(Calendar.SECOND)); System.out.println("when_reminder_date =" + when_reminder_date + " db date=" + d + " when_reminder_time=" + when_reminder_time + " dbtime=" + t); if (when_reminder_date == d &amp;&amp; t == when_reminder_time) { System.out.println("Inside if condition"); Intent intent = new Intent(this, MyBroadcastReceiver.class); intent.putExtra("when_reminder_title", when_reminder_title); PendingIntent pendingIntent = PendingIntent.getBroadcast( this.getApplicationContext(), 234324243, intent, 0); AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); alarmManager.set(AlarmManager.RTC_WAKEUP, 0, pendingIntent); c1.moveToNext(); } } c1.close(); database.close(); } public void playSound(Context context, Uri alert) { mMediaPlayer = new MediaPlayer(); try { mMediaPlayer.setDataSource(context, alert); final AudioManager audioManager = (AudioManager) context .getSystemService(Context.AUDIO_SERVICE); if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) { mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM); mMediaPlayer.prepare(); mMediaPlayer.start(); } } catch (IOException e) { System.out.println("OOPS"); } } public Uri getAlarmUri() { Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM); if (alert == null) { alert = RingtoneManager .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); if (alert == null) { alert = RingtoneManager .getDefaultUri(RingtoneManager.TYPE_RINGTONE); } } return alert; } public void addreminder_method(View v) { intent = new Intent(this, EnterWhenReminder.class); startActivity(intent); }} </code></pre> <p>Code for broadcast receiver:</p> <pre><code>import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Vibrator; public class MyBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { AlarmActivityWhen m = new AlarmActivityWhen(); String title=intent.getStringExtra("when_reminder_title"); Intent i = new Intent(context, AlertActivity.class); i.putExtra("when_reminder_title", title); context.startActivity(i); // Vibrate the mobile phone Vibrator vibrator = (Vibrator) context .getSystemService(Context.VIBRATOR_SERVICE); vibrator.vibrate(2000); m.playSound(context, m.getAlarmUri()); }} </code></pre> <p>Activity called by Broadcast Receiver:</p> <pre><code>import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.widget.TextView; public class AlertActivity extends Activity { TextView t; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_alert); Intent i=getIntent(); String title=i.getStringExtra("when_reminder_title"); t=(TextView) findViewById(R.id.title); t.setText(title); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_alert, menu); return true; }} </code></pre> <p>Update:</p> <p>I moved c1.movetonext() outside the if block. Now it is not running infinitely like it was earlier. Thank you. But I need it to run continuously in the background and check if the time to give the alarm has arrived. Where should I put the code for the pending intent so that this happens?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload