作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用广播接收器来设置警报警报,但由于某种原因它无法工作。
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 秒后获取 Toast 通知时,它不会出现。我想不出它不起作用的任何其他原因,我已经查看了 googles API 演示并密切关注它。
最佳答案
更有可能的是,您忘记将 BroadCast 接收器添加到 AndroidManifest.xml 中为了能够使用它,必须在您的 list 中声明它:
<?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>
注意:我使用此 list 快速测试了您的代码,实际上它在 10 秒后显示了 Toast...
关于java - BroadcastReceiver 未将数据发送回主类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8290795/
我是一名优秀的程序员,十分优秀!