如何创建状态栏通知,即使应用程序关闭,也会每30秒显示一次

时间:2015-07-28 11:17:45

标签: android

我想创建状态栏通知,即使应用程序关闭,也会每30秒显示一次。我搜索并实施了许多建议,但似乎没有任何建议。我能够创建通知,但是当我的应用程序未运行时,不会显示任何通知。我不希望在我的应用程序运行时显示通知。以下是我创建通知的代码:

MainActivity.class:

private void showNotification(){
        //Code for Notification

       Intent resultIntent = new Intent(this, ResultActivity.class);
       TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
       // Adds the back stack for the Intent (but not the Intent itself)
       stackBuilder.addParentStack(ResultActivity.class);
      // Adds the Intent that starts the Activity to the top of the stack
      stackBuilder.addNextIntent(resultIntent);
      PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);

      Notification notification=new Notification.Builder(this)
      .setSmallIcon(R.drawable.image1)
      .setContentTitle("Time")
      .setContentText("Time left")
      .setContentIntent(resultPendingIntent)        
      .addAction(R.drawable.logo, "View", resultPendingIntent)
      .build(); 


      NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
      notification.flags |= Notification.FLAG_AUTO_CANCEL
      notificationManager.notify(0, notification);
}


private void doTheAutoRefresh() {
    handler.postDelayed(new Runnable() {
    public void run() {               
   }
}, 30000);
}

@Override
protected void onStop() {
    super.onStop();
    showNotification();
    doTheAutoRefresh();
}

1 个答案:

答案 0 :(得分:1)

我已经提到了完整的通知代码。试试这个代码。

1]声明服务在启动画面上启动

//This is call on splash activity
new ServiceManager().start(Splash.this);

2]制作ServiceManager类

public class ServiceManager {

private AlarmManager amPMR = null;
private PendingIntent piPMR = null;
public void start(Context context) {

    Log.print("startBGProcesses ServiceManager ::  ");

    if (!AlarmReceiver.STARTED) {
        // Wake up Service
        amPMR = (AlarmManager) context
                .getSystemService(Context.ALARM_SERVICE);
        Intent intentPMR = new Intent(context, AlarmReceiver.class);
        piPMR = PendingIntent.getBroadcast(context, 0, intentPMR, 0);
        amPMR.setRepeating(AlarmManager.RTC_WAKEUP,
                System.currentTimeMillis(), (60000 * 1), piPMR);
    }

}
}

3]制作AlarmReceiver Class

public class AlarmReceiver extends BroadcastReceiver {

public static boolean STARTED = false;

private Long currentMiliSec;

@Override
public void onReceive(Context context, Intent intent) {

    AlarmReceiver.STARTED = true;

    Log.print(AlarmReceiver.class + "", "AlarmReceiver");
    this.doProcess(context);
}

public void doProcess(Context context) {
//Call your custom Notification function
Intent intentNoti = new Intent(context,Dashboard.class);
Creating_Custom_Notification_Layout(context, "Title",
                                            "SubTitle",
                                            intentNoti);
}
}

4]制作自定义通知功能和“custom_notification.xml”文件。

public static void Creating_Custom_Notification_Layout(Context mContext,
        String title, String mesg, Intent ActionIntent) {

    // int ISSUE_ID = ActionIntent.getIntExtra(Const.ISSUE_ID, 0);

    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager nManager = (NotificationManager) mContext
            .getSystemService(ns);

    nManager.cancel(0);
    // RemoteViews object is passed to the Notification in the contentView
    // field.
    // so no need to use setLatestEventInfo

    int icon = R.drawable.ic_launcher;// ic_stat_gcm;
    // CharSequence tickerText = "My Custome notification";
    long when = System.currentTimeMillis();

    Notification notification = new Notification(icon, mesg, when);

    notification.defaults = Notification.DEFAULT_VIBRATE
            | Notification.DEFAULT_SOUND;

    // 1.create custom layout of notification using RemoteView
    // 2.RemoteViews inflate custom layout and pass to contentView of your
    // notification
    // 3.See the custom_notification then
    // 4.use the RemoveViews methods to define the image and text.
    // 5.Then pass the RemoteViews object to the contentView field of the
    // Notification, as show in below

    RemoteViews contentView = new RemoteViews(mContext.getPackageName(),
            R.layout.custom_notification);
    contentView.setTextViewText(R.id.NotoficationTitle, title);
    contentView.setTextViewText(R.id.text, mesg);

    notification.contentView = contentView;

    Intent notificationIntent = new Intent();
    notificationIntent = ActionIntent;

    PendingIntent mPendingIntent = PendingIntent.getActivity(mContext, 0,
            notificationIntent,
            notification.flags |= Notification.FLAG_AUTO_CANCEL
                    | Notification.FLAG_SHOW_LIGHTS);// PendingIntent.FLAG_CANCEL_CURRENT);

    // pass pending intent to content intent to notification intent
    notification.contentIntent = mPendingIntent;

    // set notification to notification manager
    nManager.notify(0, notification);
}

5]在Manifest.xml文件中声明接收者名称

<application..
<receiver android:name="com.services.AlarmReceiver" />
</application>

我希望这个完整的代码对你有所帮助..谢谢.. :))