在android中无限运行广播接收器

时间:2012-10-26 11:25:30

标签: android broadcastreceiver

在我的项目中,我有一个包含提醒的数据库。我想比较存储在数据库中的日期和时间与当前日期和时间,并发出警报。我已经使用了广播接收器。我面临的问题是接收器无限运行并且不允许活动进入屏幕。此外,我已检查日期和时间是否匹配if条件,并仅在匹配时调用接收器。即使条件为假,它也会进入条件并启动接收器。这是我的代码。任何帮助将不胜感激。谢谢!

从数据库中检索值,比较值和启动接收器的代码:

    public class DisplayReminderList extends Activity {

ListView lv;
Intent intent;
public MediaPlayer mMediaPlayer;
ArrayAdapter<String> 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<String>(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 && 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);
}}

广播接收机代码:

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

}}

广播接收者调用的活动:

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

更新

我在if块之外移动了c1.movetonext()。现在它不像以前那样无限运行。谢谢。但我需要它在后台持续运行并检查是否有时间发出警报。我应该在哪里放置待处理意图的代码,以便发生这种情况?

0 个答案:

没有答案