gpt4 book ai didi

java - BroadcastReceiver 未将数据发送回主类

转载 作者:行者123 更新时间:2023-12-01 15:39:34 29 4
gpt4 key购买 nike

我正在使用广播接收器来设置警报警报,但由于某种原因它无法工作。

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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com