android - 如何设置报警信息?

标签 android

In alarm app, user gives input String for alarm message and sets alaram. I am able to set and listen alarm.but how to display message set by user? have set many times to set alarm, how can i display respective message for that specific alarm

最佳答案

您的 BroadcastReceiver 中应该有一个 onReceive(Context context, Intent intent) 函数。它看起来像:

public void onReceive(Context context, Intent intent) {   
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, ""); // Second argument is an arbitrary String tag
    wl.acquire();

    // Put YOUR code here (between WaitLocks)
    String userInputtedString = intent.getBundleExtra("UserText")
    Toast.makeText(context, userInputtedString, Toast.LENGTH_LONG).show();

    wl.release();
}

为了获得该 Bundle,您必须在创建警报时额外传递一个 intent

Intent intent = new Intent(context, AlarmNotification.class);
intent.putExtra("UserText", userInputedString);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

我刚刚展示了一个 Toast 消息示例,但您也可以将其更改为 Notification 或 AlertDialog。 如果这有帮助,请告诉我。

关于android - 如何设置报警信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10978367/

相关文章:

android - 为 Android 构建内核模块

java - LibGdx 桌面启动器错误 : main class not found

java - 为Android应用程序设置声音

android - 防止edittext为空

java - 如何在 android 中使用 UnboundID LDAP?

java - 无法编辑 mp3 标签

android - 无论用户位置设置如何,如何确定 Android 设备的当前国家/地区位置?

android - 从 Facebook 上的 PlayStore 共享应用程序会在缩略图中显示泰语字符?

android - STL 字符串赋值 NDK 崩溃

使用 Cordova 的 Facebook Android Key Hash