android - 我的应用程序在 android 模拟器中运行良好,但在我的真实设备中无法运行

标签 android notifications

我的应用程序应该在预定义的时间启动通知。该应用程序在模拟器中运行良好。

当我在我的 Note 3 中测试它时(在 Lollipop 5.0 中工作),通知根本没有显示..!!

我不知道是什么问题,因为它在模拟器中工作正常。

这是我构建和触发通知的方法:

 private static String customTime = "13:08:00"; // ( Temp ) for test app
    private final static String[] NOTIY_TIMES  = {customTime,"10:00:00","11:00:00","12:00:00","13:00:00","14:00:00","15:00:00","16:00:00","17:00:00","18:00:00","19:00:00","20:00:00","21:00:00","22:00:00"};

        // This Method will build , fire notification then increase the notify counter;
    public static void showNotification( Context context) {
        // Notification Builder
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
                .setColor(context.getResources().getColor(R.color.colorPrimary))
                .setSmallIcon(R.drawable.ic_water_cup)
                .setLargeIcon(largeIcon(context))
                .setStyle(new NotificationCompat.BigTextStyle().bigText("Its Nice Time To Drink Cup Of Water Now.."))
                .setContentTitle("Drinking Water = Healthey Life")
                .setContentText("Its Nice Time To Drink Cup Of Water Now..")
                .setContentIntent(openAppWhenClick(context))
                .setDefaults(NotificationCompat.DEFAULT_SOUND | NotificationCompat.DEFAULT_VIBRATE)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setAutoCancel(true)
                .addAction(acceptToDrinkAction(context))
                .addAction(cancelNotificationAction(context));

        // Fire Notification
        NotificationManager notificationManager = (NotificationManager)context.getSystemService(context.NOTIFICATION_SERVICE);
        if (notificationManager != null) {
            notificationManager.notify(NOTIFY_ID , mBuilder.build());
        }

这是我在上面的预定义时间触发通知的方法:

//该方法在一天中的特定时间午餐通知 public static void fireNotificationOnCertainTime(final Context 上下文){

// Get Current Dynamic Time uses Handler
final Handler handler = new Handler(Looper.getMainLooper());
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss", Locale.US);
        String timeNow = sdf.format(new Date());
        handler.postDelayed(this ,1000);

        // Fire notification in this specific times
        if (Arrays.asList(NOTIY_TIMES).contains(timeNow)) {
            showNotification(context);
        }
    }
},10);

}

这是我的 list 文件:

     <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.islam.waterdrinkreminder">
    <uses-permission android:name="android.permission.VIBRATE"/>


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name=".Services.Reciver"/>

        <service android:name=".Services.WaterReminderJopDispatcher"
            android:exported="false">
            <intent-filter>
                <action
                    android:name="com.firebase.jobdispatcher.ACTION_EXECUTE">
                </action>
            </intent-filter>

        </service>
    </application>

</manifest>

编辑:

我想我找到了导致问题的原因,但仍然没有找到解决方法,问题 在我的代码中的时间模式中,我使用模式“HH:mm:ss”这个模式 在模拟器中工作,但我的真实手机无法识别它..!! 我尝试在两者上都使用模式“HH:mm”,但继续发送通知 多次(发送孔分钟通知因为我删除秒) 希望这有助于找到解决方案..谢谢你们。

最佳答案

我假设发生这种情况是因为您的模拟器运行的是 Android O(这改变了通知的管理方式)。要深入了解如何添加对 Android O 的支持,我建议查看 this documentation .

但简而言之,您现在需要为所有通知添加一个 channel 。以下是文档中的代码摘录:

NotificationManager mNotificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// The id of the channel.
String id = "my_channel_01";
// The user-visible name of the channel.
CharSequence name = getString(R.string.channel_name);
// The user-visible description of the channel.
String description = getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(id, name, importance);
// Configure the notification channel.
mChannel.setDescription(description);
mChannel.enableLights(true);
// Sets the notification light color for notifications posted to this
// channel, if the device supports this feature.
mChannel.setLightColor(Color.RED);
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
mNotificationManager.createNotificationChannel(mChannel);

[编辑]

我看到您提到 SimpleDateFormat 不工作,实际上我不确定为什么会这样。但在单独的说明中,我不建议像现在这样对通知进行计时(使用有效时间数组和延迟 10 毫秒的处理程序)。

在 Android 中,您可以访问 AlarmManager .可以找到有关如何实现重复警报的文档 here .

使用 AlarmManager,您可以根据耗时关闭闹钟,或者您可以让闹钟在非常特定的时间唤醒。上面的链接应该向您展示它是如何工作的。下面是一个小代码示例,显示了一种可能的解决方案,该解决方案将有一个每小时触发一次服务的警报。

    alarmMgr.setInexactRepeating(
            // Wake the device based on elapsed time
            AlarmManager.ELAPSED_REALTIME_WAKEUP,
            // Trigger 30 minutes after this call
            SystemClock.elapsedRealtime() + AlarmManager.INTERVAL_HALF_HOUR,
            // Keep triggering every hour
            AlarmManager.INTERVAL_HOUR,
            // The intent to trigger - likely waking some Service to do a job
            alarmIntent);

AlarmManager 的其他用途让您可以设置具体的启动时间。更多用于调度的Android组件选项,可以查看this page of the documentation .

如果您真的想继续使用Handler,您可以让您的时间数组实际上是一个时间映射,它对应于一个 bool 值,表示如果通知已经发送。然后这张 map 可以在一天结束时重置(或者一旦最后一次被击中)。或者还有大量其他方法可以优化 Handler/Array 方法 - 这只是一个需要最少更改的想法。无论如何,我建议使用 AlarmManager,因为从长远来看,它占用的资源更少,而且更准确。

关于android - 我的应用程序在 android 模拟器中运行良好,但在我的真实设备中无法运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48349614/

相关文章:

android - 前台服务通知需要几秒钟才能显示

android - 收到推送通知时添加新通知(不替换以前的)

android - 在android模拟器中安装操作系统

android - 以编程方式将位置分配给android中的按钮

android - Android 中不显示弹出窗口的背景

android - 如何在收到的 Android 通知中设置默认的 smallIcon 和 largeIcon?

javascript - 如何创建在全屏应用程序上显示的 Javascript/Chrome 通知

android - 如何动态添加单选按钮而不丢失 View 内容

android - 如果我的 Android 项目只有 v21 布局,它会支持旧设备吗?

mysql - Node.js 站点上的通知系统使用什么数据库/技术?