【问题标题】:How to set Notification locally using Service and Broadcastreceiver?如何使用服务和广播接收器在本地设置通知?
【发布时间】:2014-01-10 14:36:55
【问题描述】:

我是安卓新手。我想在本地设置通知,每天在特定时间触发它。当我启动服务时...通知立即触发...

我想知道如何在特定时间触发通知而不立即触发

第 1 步:

public class startservice extends Activity {

     @Override
     public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      Button button=(Button) findViewById(R.id.button1);
      button.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(startservice.this,MainActivity.class);
            startservice.this.startService(intent);
        }
    });

     }

    }

第 2 步:启动服务

public class MainActivity extends Service {

     AlarmManager am;
     Calendar calendar;
     int count=0;


     @Override
     public void onCreate() {
      super.onCreate();

      am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
             calendar=Calendar.getInstance();
             setNotification();

     }
     public void setNotification() {
          Intent intent = new Intent(this, Startnotification.class);
          calendar.set(Calendar.HOUR, 9); // At the hour you wanna fire
          calendar.set(Calendar.MINUTE,12); // Particular minute
          calendar.set(Calendar.SECOND, 0);
          double x = Math.random();
          String value=String.valueOf(x);
          intent.putExtra("name", value);
          sendBroadcast(intent);
          PendingIntent pendingIntent = PendingIntent.getBroadcast(this, count,intent, PendingIntent.FLAG_UPDATE_CURRENT);       
          am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),3600000,pendingIntent);
        count++;
         }
    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

}

第三步:广播接收器

public class Startnotification extends BroadcastReceiver {

     NotificationManager nm;


     @SuppressWarnings("deprecation")
    @Override
     public void onReceive(Context context, Intent intent) {
      nm = (NotificationManager) context
        .getSystemService(Context.NOTIFICATION_SERVICE);
      String getvalue= intent.getStringExtra("name");
      CharSequence from = getvalue;
      CharSequence message = "Android calendar...";
      PendingIntent contentIntent = PendingIntent.getService(context, 0, new Intent(),0);

      Notification notif = new Notification(R.drawable.ic_launcher,"Android calendar...", System.currentTimeMillis());
      notif.setLatestEventInfo(context, from, message, contentIntent);
      nm.notify(1, notif);
      System.out.println("BroadcastReceiver");
     }
    }

如何实现

在此先感谢...

【问题讨论】:

    标签: android android-intent android-service android-pendingintent android-broadcast


    【解决方案1】:

    您正在拨打setNotification()

          sendBroadcast(intent);
    

    这会立即致电您的Startnotification.onReceive()

    另外,您可以像这样在 Calendar 实例中设置 HOUR:

    calendar.set(Calendar.HOUR, 9); // At the hour you wanna fire
    

    但是您没有明确地将 AM/PM 指示器设置为 12 小时制(HOUR 用于设置 12 小时制)。这意味着 AM/PM 指示器仍将设置为您调用 Calendar.getInstance() 时的状态。例如,如果您在上午 10:00 运行此代码,并将 HOUR 设置为 9,则警报将立即响起,因为时间已经过去。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多