广播接收器没有发回数据,主类 [英] BroadcastReceiver not sending back data to main class

查看:84
本文介绍了广播接收器没有发回数据,主类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用一个BroadcastReceiver来设置一个报警alaret,由于某种原因,它不会工作。

I am using a BroadcastReceiver to set up an alarm alaret and for some reason it won't work.

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

    }
};



}

我的其他类是这个

my other class is this

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秒就不会出现后Toast通知。我想不出任何其他原因,它不会工作,我已经看过Google的API演示和遵循它密切。

When I try to get that toast notification after 10 sec it won't appear. I can't think of any other reason that it won't work and I have looked at googles API demos and followed it closely.

推荐答案

更​​可能的是,你忘了给广播接收机添加到您的Andr​​oidManifest.xml
为了能够使用它,它必须在清单中声明:

More likely, you forgot to add the BroadCast Receiver to your AndroidManifest.xml To be able to use it, it must be declared in your manifest:

<?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>

请注意:我很快就测试了code,这一清单,而事实上后10秒显示吐司...

NOTE: I quickly tested your code, with this manifest, and indeed it shows the Toast after 10 secs...

这篇关于广播接收器没有发回数据,主类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆