有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java创建Android警报通知

如何将我的通知作为警报,目前我只在应用程序中收到通知

主要活动:

public void sendNotificationIfTimeEnd01() {
    Context context = getApplicationContext();
    Intent intent01 = new Intent(context, MainActivity.class);
    PendingIntent pendingIntent01 = PendingIntent.getActivity(this, 1, intent01, 0);
    NotificationCompat.Builder builder01 = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_stat_notification)
            .setContentIntent(pendingIntent01)
            .setAutoCancel(true)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
            .setContentTitle(gamesJuliToStringArray[0])
            .setContentText("ready")
            .setSubText("clkick");
    NotificationManager notificationManager = (NotificationManager) getSystemService(
            NOTIFICATION_SERVICE);
    notificationManager.notify(NOTIFICATION_ID_01, builder01.build());
    try {
        Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
        r.play();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

共 (1) 个答案

  1. # 1 楼答案

    也许你应该在退出应用程序时,在后台运行该服务。当我读到你的描述时,似乎你把它放在了一个活动中。当你回到家时,应用程序处于暂停状态。我建议您运行后台服务

    请尝试检查:Service

    我只是试着从谷歌编辑教程

    public class HelloIntentService extends IntentService {
    
      /**
       * A constructor is required, and must call the super IntentService(String)
       * constructor with a name for the worker thread.
       */
      public HelloIntentService() {
          super("HelloIntentService");
      }
    
    
      @Override
      protected void onHandleIntent(Intent intent) {
          //maybe some works you need to do
          //sleep for the time until you want the notification
          //put you notification code here
      }
    }
    

    此方法仅适用于在作业完成之前必须执行的工作,并将通知发送给报警用户的情况

    但是如果你正在做一个使用通知的闹钟。也许你也可以尝试使用类似AlarmManager、Receiver之类的东西来实现。我在这个网页上找到了一个例子: http://javapapers.com/android/android-alarm-clock-tutorial/

    对不起,我的英语不好,希望这能帮助你