我希望调用状态栏的通知

时间:2014-05-27 18:49:06

标签: android notifications

如何在特定时间在状态栏中获取通知,我尝试了这一点,但每个特定时间都会调用pendingIntents活动。我希望调用状态栏的通知。请帮忙

public class MainActivity extends ActionBarActivity {

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_main);
    Notification("abduct: ","kidnap");

}   
@SuppressWarnings("deprecation")
private void Notification(String notificationTitle, String notificationMessage)
{
    NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); 
 Intent notificationIntent = new Intent(this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
       Notification notification = new Notification(R.drawable.icon, "First word", 50000);
    notification.setLatestEventInfo(MainActivity.this, notificationTitle, notificationMessage, null);
    notificationManager.notify(1, notification);
    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
    alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), 1*60*60 , pendingIntent );  //set repeating every 24 hours

}
}

1 个答案:

答案 0 :(得分:0)

问题在于您提供PendingIntent的{​​{1}}。通过AlarmManager创建它,实际上是指示它在指定时间过后开始您的活动

您需要做的是:

  • 创建PendingIntent.getActivity()并在AndroidManifest.xml文件中对其进行配置。
  • 而不是您目前正在做的事情,为BroadcastReceiver提供AlarmManager向该接收者发送广播(PendingIntent)。
  • 在接收者的PendingIntent.getBroadcast()中,构建并显示通知。

您可以找到简明扼要的教程here

相关问题