如何在android中生成每小时显示的通知栏?

时间:2014-03-15 06:59:13

标签: notifications

我想创建一个每小时生成一次的通知列表,其中包含后台运行的应用程序列表。 如何做到这一点。 PLZ建议我正确的方式,我没有得到正确的。

1 个答案:

答案 0 :(得分:0)

正确答案是:

public class NotificationGnt extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.noti);

    Intent intr= getIntent();

    MyTimerTask myTask = new MyTimerTask();
    Timer myTimer = new Timer();

   // After 2 mins 

    myTimer.schedule(myTask, 300000, 120000);

}

class MyTimerTask extends TimerTask {
    public void run() {
        generateNotification(getApplicationContext(), "Running Apps......!");
    }
}

private void generateNotification(Context context, String message) {

    int icon = R.drawable.pro;
    long when = System.currentTimeMillis();
    String appname = context.getResources().getString(R.string.app_name);
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    int currentapiVersion = android.os.Build.VERSION.SDK_INT;
    Notification notification;
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
            new Intent(context, Secondmain.class), 0);
    System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");


    // To support 2.3 os, we use "Notification" class and 3.0+ os will use
    // "NotificationCompat.Builder" class.
    if (currentapiVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {
        notification = new Notification(icon, message, 0);
        notification.setLatestEventInfo(context, appname, message,
                contentIntent);
        notification.flags = Notification.FLAG_AUTO_CANCEL;
        notificationManager.notify((int) when, notification);
        System.out.println("%kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk");
    } else {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        notification = builder.setContentIntent(contentIntent)
                .setSmallIcon(icon).setTicker(appname).setWhen(0)
                .setAutoCancel(true).setContentTitle(appname)
                .setContentText(message).build();

        notificationManager.notify((int) when, notification);
        System.out.println("%###################################");

    }
  }
}