BroadcastReceiver不向主类发回数据

时间:2011-11-28 02:06:50

标签: java android

我正在使用BroadcastReceiver来设置闹钟,因为某些原因它无法正常工作。

public class SimpleSleepActivity extends Activity {
/** Called when the activity is first created. */

Button setAlarm, setTimer;
int hours = 1, alarmSec = 10;
Toast mToast;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    setAlarm = (Button) findViewById(R.id.bSetAlarm);
    setTimer = (Button) findViewById(R.id.bSetTimer);
    setTimer.setOnClickListener(mAlarmFromNow);


}

private OnClickListener mAlarmFromNow = new OnClickListener() {
    public void onClick(View v) {

        // When the alarm goes off, broadcast an Intent to the 
        // BroadcastReceiver. This is an Intent with an explicit class
        // name to have a receiver instantiated and called, and then 
        // create an IntentSender to have the intent executed as a broadcast.
        Intent intent = new Intent(SimpleSleepActivity.this,
                AlarmFromNow.class);
        PendingIntent sender = PendingIntent.getBroadcast(
                SimpleSleepActivity.this, 0, intent, 0);

        // Finding the current time and setting and alarm for XX seconds
        // from that time
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.add(Calendar.SECOND, alarmSec);

        // Scheduling the alarm
        AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
        am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);

        if (mToast != null) {
            mToast.cancel();
        }
        mToast = Toast.makeText(SimpleSleepActivity.this,
                //Show the user what they imputed
                "The alarm will go off in " + alarmSec + " Seconds", Toast.LENGTH_LONG);
        mToast.show();

    }
};



}

我的另一课是这个

public class AlarmFromNow extends BroadcastReceiver {

public void onReceive(Context context, Intent intent) {
    //Display a toast after alarmSec is counted
    Toast.makeText(context, R.string.alarm_from_now, Toast.LENGTH_LONG)
            .show();
}

}

当我尝试在10秒后获得吐司通知时,它将不会出现。我想不出任何其他原因,它将无法工作,我已经看了谷歌API演示并密切关注它。

2 个答案:

答案 0 :(得分:1)

更有可能的是,您忘记将BroadCast Receiver添加到AndroidManifest.xml 为了能够使用它,必须在清单中声明:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.yourpackage.name"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="9" />
    <uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >

        <!-- Your activity here -->
        <activity android:name="SimpleSleepActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
             </intent-filter>
        </activity>

        <!-- Your Receiver here -->
        <receiver android:name="AlarmFromNow"></receiver>
    </application>
</manifest>

注意:我使用此清单快速测试了您的代码,并且确实在10秒后显示了Toast ...

答案 1 :(得分:0)

我遇到了类似的事情。我必须在Intent上设置一个操作才能使其正常工作。传递给setAction()的值无关紧要。尝试...

Intent intent = new Intent(SimpleSleepActivity.this,
            AlarmFromNow.class);
intent.setAction("Doesn't Really Matter");
PendingIntent sender = PendingIntent.getBroadcast(
            SimpleSleepActivity.this, 0, intent, 0);