BroadcastReceiver 和 AlarmManager Android

编程入门 行业动态 更新时间:2024-10-24 00:22:07
本文介绍了BroadcastReceiver 和 AlarmManager Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用带有 BroadcastReceiver 的警报管理器.我尝试使用 教程:系统服务和 BroadcastReceiver 中给出的示例.但是当我在时间结束后运行示例时,吐司没有出现.代码如下:

I am trying to use an alarm manager with BroadcastReceiver. I try to use the example given in Tutorial: System Services and BroadcastReceiver . but when I run the example after the end of the time the toast doesn't show up. the code is below:

我的主要活动:

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

我的广播接收器:

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="schemas.android/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>

和清单文件:

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="schemas.android/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 扩展

The receiver must extend from BroadcastReceiver

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

还要确保清单文件中的接收者名称正确.

Also be sure that receiver name in manifest file is correct.

更多推荐

BroadcastReceiver 和 AlarmManager Android

本文发布于:2023-11-27 04:30:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636599.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:BroadcastReceiver   AlarmManager   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!