2015-07-22 75 views
0

我想在特定时间和日常活动中显示通知(即在8点钟的早上)。我正在使用广播接收器,警报管理器来显示通知。 但问题是,我没有收到移动设备上显示的通知。 我也在清单文件中添加了唤醒锁权限。请帮助我 谢谢在特定时间显示通知

MainActivity.java

public class MainActivity extends ActionBarActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Calendar calendar = Calendar.getInstance(); 
    calendar.set(Calendar.HOUR_OF_DAY, 8); 
    calendar.set(Calendar.MINUTE, 00); 
    calendar.set(Calendar.SECOND, 0); 

    Intent notificationmassage = new Intent(getApplicationContext(),NotificationClass.class); 

    //This is alarm manager 
    PendingIntent pi = PendingIntent.getService(this, 0 , notificationmassage, PendingIntent.FLAG_UPDATE_CURRENT); 
    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); 
    am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 
      AlarmManager.INTERVAL_DAY, pi); 

    Toast.makeText(this, "Start Alarm", Toast.LENGTH_LONG).show(); 

} 

NotificationClass.java

public class NotificationClass extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     // TODO Auto-generated method stub 

     PendingIntent contentIntent = PendingIntent.getActivity(context, 0, 
       new Intent(context, MainActivity.class), 0); 

     NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context) 
               .setSmallIcon(R.drawable.ic_launcher) 
               .setContentTitle("Text1") 
               .setContentText("Text2 "); 
     mBuilder.setContentIntent(contentIntent); 
     mBuilder.setDefaults(Notification.DEFAULT_SOUND); 
     mBuilder.setAutoCancel(true); 
     NotificationManager mNotificationManager ; 
     mNotificationManager= (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); 
     mNotificationManager.notify(1,mBuilder.build()); 

    } 
} 

清单:

<uses-sdk 
    android:minSdkVersion="8" 
    android:targetSdkVersion="21" /> 

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


<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> 

    <receiver android:name=".NotificationClass"> 
     </receiver> 
</application> 

UPDAT E: - 现在问题出现在我打开应用程序通知时出现。对于例如:警报设置为8.AM,每当我在上午8点之后打开应用程序,就会发出通知。即使当前时间>上午8点。如何解决这个问题?

回答

1

在你的活动替换以下

PendingIntent pi = PendingIntent.getService(this, 0, notificationmassage, PendingIntent.FLAG_UPDATE_CURRENT); 

PendingIntent pi = PendingIntent.getBroadcast(this, 0, notificationmassage, PendingIntent.FLAG_UPDATE_CURRENT); 
+0

好的。当我打开应用程序时会显示通知。当我关闭应用程序时,它不会显示它。我认为它不是作为一个进程在后台运行 – Karan

+0

如何将它作为后台进程运行..这样即使我的应用程序关闭,它也会在特定时间显示通知? – Karan

+1

您正在使用AlarmManager.RTC_WAKEUP,因此,一旦您调用am.setRepeating(...),它应该在所需的时间唤醒设备。自动执行接收器并显示通知。 – DaniZ

0
 public void getNotification(Context context,String Message){  
      int icon = R.drawable.appicon; 
      int when =(int) System.currentTimeMillis(); 
      NotificationManager notificationManager = (NotificationManager) 
        context.getSystemService(Context.NOTIFICATION_SERVICE); 
      Notification notification = new Notification(icon, Message, when); 

      String title = context.getString(R.string.app_name); 

      Intent notificationIntent = new Intent(context, MainActivity.class); 

      // set intent so it does not start a new activity 
      notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |Intent.FLAG_ACTIVITY_CLEAR_TASK); 
      PendingIntent intent =PendingIntent.getActivity(context, 0, notificationIntent, 0); 
      notification.setLatestEventInfo(context, title,Message, intent); 
      notification.flags |= Notification.FLAG_AUTO_CANCEL; 

      // Play default notification sound 
      notification.defaults |= Notification.DEFAULT_SOUND; 

      // Vibrate if vibrate is enabled 
      notification.defaults |= Notification.DEFAULT_VIBRATE; 
      notificationManager.notify(0, notification); 

    } 
+0

我想每天或常规像@ 8'o时钟在对特定时间显示通知早上。怎么做? – Karan

+0

如何将它作为后台进程运行..以便即使我的应用程序关闭,它也会在特定时间显示通知? – Karan