【问题标题】:unable to get full working code for android notification无法获得 android 通知的完整工作代码
【发布时间】:2014-03-07 17:36:25
【问题描述】:

我想在某个时间间隔后自动显示和 android 通知

我使用了all suggested code snippets,但我在 5 分钟后使用 failed to get automatic notification

 public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
          String message = "Hellooo, alrm worked ----";
          Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
          Intent intent2 = new Intent(context, TripNotification.class); 
          intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          context.startActivity(intent2);
    }

    public void setAlarm(Context context){
        Log.d("Carbon","Alrm SET !!");

        // get a Calendar object with current time
         Calendar cal = Calendar.getInstance();
         // add 30 seconds to the calendar object
         cal.add(Calendar.SECOND, 30);
         Intent intent = new Intent(context, AlarmReceiver.class);
         PendingIntent sender = PendingIntent.getBroadcast(context, 192837, intent, PendingIntent.FLAG_UPDATE_CURRENT);

         // Get the AlarmManager service
         AlarmManager am = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
         am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
    }
}


 <receiver  android:process=":remote" android:name="AlarmReceiver"></receiver>

【问题讨论】:

  • 您应该使用BroadcastReceiver 和广播Intent 而不是Activity
  • 非常感谢,但是怎么做?我是安卓新手。你能给我任何工作代码sn-p
  • 只需查看PendingIntentAlarmManager 文档。您只需要创建一个广播挂起的意图并在您的代码中创建一个BroadcastReceiver 并在您的清单中声明。然后,您的接收者可以更新通知。
  • 我看过文档,但不明白在哪里调用代码片段:(
  • 你的接收者注册的intent-filter是什么?

标签: android android-layout android-intent android-notifications


【解决方案1】:

我建议你写一个Activity。在 onCreate() 方法中每 5 分钟设置一次闹钟,如下所示:

Intent intent = new Intent(this, ServiceForAlarm.class);
intent.setAction(""+System.currentTimeMillis());

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getService(m_Context, 0, intent, Intent.FLAG_GRANT_READ_URI_PERMISSION);
alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, System.currentTimeMillis(), 300000L, pendingIntent);

ServiceForAlarm 中,编写代码来推送通知。

public class ServiceForAlarm extends Service {
        private Context mContext;

    @Override
    public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() {
    // TODO Auto-generated method stub
        super.onCreate();
            mContext = getApplicationContext();
    }

    @Override
    public void onStart(Intent intent, int startId) {
        // TODO Auto-generated method stub
        super.onStart(intent, startId);
        SetNotification();
        stopSelf();
    }

private void SetNotification() {
    m_NotificationManager = (NotificationManager)m_Context.getSystemService(Context.NOTIFICATION_SERVICE);
    m_Notification = new Notification(R.drawable.icon_app, "Your message", System.currentTimeMillis());
    m_Intent = new Intent() ;
    m_PendingIntent = PendingIntent.getActivity(m_context, 0, m_Intent, 0);
    m_Notification.contentIntent = m_PendingIntent;
    m_Notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE | Notification.FLAG_AUTO_CANCEL;
    m_Notification.setLatestEventInfo(m_context, "Title", "Click me", m_PendingIntent);
    m_NotificationManager.notify(1010, m_Notification);
}

}

【讨论】:

  • 有完整的sn-p代码吗? ServiceForAlarm的代码在哪里
猜你喜欢
  • 1970-01-01
  • 2022-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-22
  • 1970-01-01
  • 2020-07-12
相关资源
最近更新 更多