【问题标题】:Create Android Alarm notification创建 Android 警报通知
【发布时间】:2015-09-20 13:36:16
【问题描述】:

我如何将我的通知作为警报,目前我只有在应用程序内时才会收到通知。

主活动:

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

【问题讨论】:

    标签: java android alarmmanager android-notifications android-alarms


    【解决方案1】:

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

    试试看:Service

    我只是尝试从 Google 编辑教程

    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/

    对不起,我的英语不好,希望这可以帮助你。

    【讨论】:

    • 感谢您的回答,在后台运行服务的链接中找不到任何内容。你有一个 sn-p 来完成这个吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多