【问题标题】:Notification not triggering通知未触发
【发布时间】:2014-04-02 00:16:06
【问题描述】:

我在 stackoverflow 上阅读了近 30 个线程,但我无法让这段代码正常工作。我每天下午 14 点都需要通知。我在 Logcat 中没有得到任何错误,只有“waitforAlarm 结果:某个整数”。我很绝望……

这是我的代码:

清单:

    <uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />

Notifications.class

public class Notifications extends Service {

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate(){

    NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    Intent notificationIntent = new Intent(this, Main.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0 , notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    Notification notification = new Notification(R.drawable.icona, "huhu", System.currentTimeMillis());


    notification.defaults |= Notification.DEFAULT_SOUND;
    notification.setLatestEventInfo(this, "asdas", "huhu", contentIntent);
    mNotificationManager.notify(0, notification);

}

}

主类

public void AlarmMethod(){

    Intent myIntent = new Intent(this, Notifications.class);
    AlarmManager alarmManager = (AlarmManager) this.getSystemService(this.ALARM_SERVICE);
    PendingIntent pendingIntent = PendingIntent.getService(this, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    Calendar calendar = Calendar.getInstance();
    //calendar.set(Calendar.SECOND, 0);
    //calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.HOUR_OF_DAY, 14);
    //calendar.set(Calendar.AM_PM, Calendar.AM);
    //calendar.add(Calendar.DAY_OF_MONTH,   1);

    alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24 * 60 * 60 * 1000, pendingIntent);
}

问题已解决:

最后我离开这个方法,我使用广播接收器:

Here is solution

【问题讨论】:

    标签: android notifications alarmmanager android-alarms


    【解决方案1】:
          @Override
        public void onCreate(){
    
         NotificationManager mNotificationManager = (NotificationManager)       getSystemService(NOTIFICATION_SERVICE);
    Intent notificationIntent = new Intent(this, Main.class);
     notificationIntent .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    
    PendingIntent pIntent = PendingIntent.getActivity(getApplicationContext(), 0,
                        notificationIntent, 0);
    
    
          Notification noti = new Notification.Builder(getApplicationContext())
                        .setContentTitle("PACS Notification")
                        .setContentText(Alarm Notification from Alarm Manager")
                        .setSmallIcon(R.drawable.icona)
                        .setContentIntent(pIntent ).build();
            noti.flags |= Notification.FLAG_AUTO_CANCEL;
                noti.defaults |= Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE;
                mNotificationManager .notify(0, noti);
    
    
    
        }
    

    注意: 请在 androidmanifest.xml 上添加你的服务类,

      <service android:enabled="true" android:name=".Notifications " />
    

    【讨论】:

    • 我尝试将其添加到清单中,但没有帮助.. 并且您的代码 eclipse 无法识别 .build() 方法,是的,我已经更新了 android 支持库...
    猜你喜欢
    • 2014-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-30
    • 2018-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多