根据Android时间发送通知

时间:2016-04-03 11:27:08

标签: android notifications

我是Android开发的新手,我需要根据以下两种情况发送通知:

  1. On" showNotification"按钮单击使用" showNotification"方法
  2. 使用" alertNotification"经过一段时间(&#34后5秒; alertNotification"我的示例中点击了按钮)方法如下:
  3. 第一个案例完美无缺。但是,我遇到了第二种情况的问题。

    我按照https://www.rosettacode.org/wiki/Dynamic_variable_names#JavaScript

    上解释的相同方式进行操作

    以下是我的代码:

    1. MainActivity.java

      public class MainActivity extends Activity {
          // Send Notification on button click
          Button showNotification;
          // Send Notification 5 second after on button click
          Button alertNotification;
          NotificationManager notificationManager;
          boolean isNotificActive = false;
      
          int notifID = 33;
      
          public static final int NOTIFICATION_ID = 1;
      
          public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.sample_layout);
      
              showNotification = (Button) findViewById(R.id.showNotification);
              alertNotification = (Button) findViewById(R.id.alerNotification);
          }
      
          public void showNotification(View view) {
              NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                      .setContentTitle("Notification")
                      .setContentText("New Notification")
                      .setTicker("Alert New Notification")
                      .setSmallIcon(R.drawable.ic_stat_notification);
      
              Intent moreInfoIntent = new Intent(this, MoreNotification.class);
              TaskStackBuilder stackBuilder = null;
              if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
                  stackBuilder = TaskStackBuilder.create(this);
      
                  stackBuilder.addParentStack(MoreNotification.class);
                  stackBuilder.addNextIntent(moreInfoIntent);
      
                  PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.    FLAG_UPDATE_CURRENT);
      
                  notificationBuilder.setContentIntent(pendingIntent);
      
                  notificationManager = (NotificationManager) getSystemService(Context.    NOTIFICATION_SERVICE);
      
                  notificationManager.notify(notifID, notificationBuilder.build());
      
                  isNotificActive = true;
              }
          }
      
          public void alertNotification(View view) {
              Long alertTime = new GregorianCalendar().getTimeInMillis()+5*1000;
              Intent alertIntent = new Intent(this, AlertReceiver.class);
      
              AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
      
              alarmManager.set(AlarmManager.RTC_WAKEUP, alertTime, PendingIntent.getBroadcast(
                      this, 1, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));
      
              Log.e("alarmManager", "alarmManager");
          }
      }
      
    2. AlertReceiver.java

      public class AlertReceiver extends BroadcastReceiver {
          @Override
          public void onReceive(Context context, Intent intent) {
              Log.e("onReceive", "onReceive");
              createNotification(context, "Times UP", "5 Seconds Has Passed", "Alert");
          }
      
          public void createNotification(Context context, String msg, String msgText, String msgAlert) {
              PendingIntent notificationIntent = PendingIntent.getActivity(context, 0,
                      new Intent(context, MainActivity.class), 0);
      
              NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
                      .setSmallIcon(R.drawable.ic_stat_notification)
                      .setContentTitle(msg)
                      .setTicker(msgAlert)
                      .setContentText(msgText);
      
              mBuilder.setContentIntent(notificationIntent);
              mBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);
              mBuilder.setAutoCancel(true);
      
              NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(      Context.NOTIFICATION_SERVICE);
      
              mNotificationManager.notify(1, mBuilder.build()); 
          }
      }
      
    3. 的AndroidManifest.xml

      <!-- Min/target SDK versions (<uses-sdk>) managed by build.gradle -->
      
      <uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
      
      <application
          android:allowBackup="true"
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme">
          <activity
              android:name=".MainActivity"
              android:label="@string/app_name">
              <intent-filter>
                  <action android:name="android.intent.action.MAIN" />
      
                  <category android:name="android.intent.category.LAUNCHER" />
              </intent-filter>
          </activity>
      
          <activity
              android:name=".MoreNotification"
              android:label="More Notification"
              android:parentActivityName=".MainActivity">
              <meta-data
                  android:name="android.support.PARENT_ACTIVITY"
                  android:value=".MainActivity"/>
      
      
          </activity>
      </application>
      

1 个答案:

答案 0 :(得分:1)

AlertReceiver必须在AndroidManifest.xml注册。

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">

    <!-- Your stuff here. -->

    <receiver android:name=".AlertReceiver"/>

</application>