gpt4 book ai didi

android - BroadcastReceiver 和 AlarmManager Android

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:37:30 26 4
gpt4 key购买 nike

我正在尝试将警报管理器与 BroadcastReceiver 一起使用。我尝试使用 Tutorial: System Services and BroadcastReceiver 中给出的示例.但是当我在时间结束后运行示例时, toast 没有出现。代码如下:

我的主要 Activity :

public class AlarmActivity extends Activity {


/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarm);
}

public void startAlert(View view) {
EditText text = (EditText) findViewById(R.id.time);
int i = Integer.parseInt(text.getText().toString());
Intent intent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
+ (i * 1000), pendingIntent);
Toast.makeText(this, "Alarm set in " + i + " seconds",
Toast.LENGTH_LONG).show();
}


}

我的 broadCastReceiver:

public class MyBroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Don't panic but your time is up!!!!.",
Toast.LENGTH_LONG).show();
// Vibrate the mobile phone
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(2000);
}

}

我的主要布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<EditText
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Number of seconds"
android:inputType="numberDecimal" >
</EditText>

<Button
android:id="@+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="startAlert"
android:text="Start Counter" >
</Button>

</LinearLayout>

和 list 文件:

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

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.VIBRATE" >
</uses-permission>

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.broadcastreceiver.AlarmActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.example.android_alarm.MyBroadcastReceiver" >
</receiver>
</application>

</manifest>

最佳答案

接收器必须从 BroadcastReceiver 扩展

public class MyBroadcastReceiver extends BroadcastReceiver {
// ...
}

还要确保 list 文件中的接收者名称是正确的。

关于android - BroadcastReceiver 和 AlarmManager Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21429726/

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