如何创建它,即使在应用程序关闭,每隔30秒显示状态栏通知 [英] How to create status bar notification which is displayed every 30 seconds even when the application is closed

查看:126
本文介绍了如何创建它,即使在应用程序关闭,每隔30秒显示状态栏通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建一个显示每30秒,即使在应用程序关闭状态栏通知。我已经搜索并实施了很多建议,但没有一个似乎工作。我能够创建通知,但是当我的应用程序没有运行,不显示任何通知。我不希望我的应用程序运行时要显示的通知。下面是我的code创建通知:

MainActivity.class:

 私人无效showNotification(){
        // code的通知       意图resultIntent =新意图(这一点,ResultActivity.class);
       TaskStackBuilder stackBuilder = TaskStackBuilder.create(本);
       //添加后退堆栈的意图(而不是意图本身)
       stackBuilder.addParentStack(ResultActivity.class);
      //添加启动该活动到堆栈的顶部的意图
      stackBuilder.addNextIntent(resultIntent);
      的PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);      通知通知=新Notification.Builder(本)
      .setSmallIcon(R.drawable.image1)
      .setContentTitle(时代)
      .setContentText(剩余时间)
      .setContentIntent(resultPendingIntent)
      .addAction(R.drawable.logo,查看,resultPendingIntent)
      。建立();
      NotificationManager notificationManager =(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
      notification.flags | = Notification.FLAG_AUTO_CANCEL
      notificationManager.notify(0,通知);
}
私人无效doTheAutoRefresh(){
    handler.postDelayed(新的Runnable(){
    公共无效的run(){
   }
},30000);
}@覆盖
保护无效的onStop(){
    super.onStop();
    showNotification();
    doTheAutoRefresh();
}


解决方案

我不得不提的充分通知code。试试这个code。

1]声明服务开始启动画面

  //这是飞溅活动电话
新的ServiceManager()开始(Splash.this);

2]制作的ServiceManager类

 公共类的ServiceManager {私人AlarmManager amPMR = NULL;
私人的PendingIntent piPMR = NULL;
公共无效启动(上下文的背景下){    Log.print(startBGProcesses的ServiceManager ::);    如果(!AlarmReceiver.STARTED){
        //叫醒服务
        amPMR =(AlarmManager)上下文
                .getSystemService(Context.ALARM_SERVICE);
        意图int​​entPMR =新意图(背景下,AlarmReceiver.class);
        piPMR = PendingIntent.getBroadcast(上下文,0,intentPMR,0);
        amPMR.setRepeating(AlarmManager.RTC_WAKEUP,
                System.currentTimeMillis的(),(60000 * 1),piPMR);
    }}
}

3]让AlarmReceiver类

 公共类AlarmReceiver扩展广播接收器{公共静态布尔已启动= FALSE;私人长期currentMiliSec;@覆盖
公共无效的onReceive(上下文的背景下,意图意图){    AlarmReceiver.STARTED = TRUE;    Log.print(AlarmReceiver.class +,AlarmReceiver);
    this.doProcess(上下文);
}公共无效doProcess(上下文的背景下){
//调用自定义通知功能
意图int​​entNoti =新意图(背景下,Dashboard.class);
Creating_Custom_Notification_Layout(背景下,标题,
                                            字幕,
                                            intentNoti);
}
}

4]让你的自定义通知功能和custom_notification.xml文件。

 公共静态无效Creating_Custom_Notification_Layout(上下文mContext,
        标题字符串,字符串MESG,意图ActionIntent){    // INT ISSUE_ID = ActionIntent.getIntExtra(Const.ISSUE_ID,0);    字符串NS = Context.NOTIFICATION_SERVICE;
    NotificationManager nManager =(NotificationManager)mContext
            .getSystemService(纳秒);    nManager.cancel(0);
    // RemoteViews对象传递给通知的内容查看
    //字段。
    //所以没有必要使用setLatestEventInfo    INT图标= R.drawable.ic_launcher; // ic_stat_gcm;
    // CharSequence的tickerText =我的Custome的通知;
    时长= System.currentTimeMillis的();    通知通知=新的通知(图标,MESG时);    notification.defaults = Notification.DEFAULT_VIBRATE
            | Notification.DEFAULT_SOUND;    使用远程视窗通知// 1,创建自定义布局
    // 2.RemoteViews膨胀的自定义布局,并传递到的内容查看您的
    //通知
    // 3.See的custom_notification然后
    // 4.使用RemoveViews方法来定义图像和文本。
    // 5.Then传递RemoteViews对象的的内容查看现场
    //通知,如显示在下面    RemoteViews内容查看=新的RemoteViews(mContext.getPackageName()
            R.layout.custom_notification);
    contentView.setTextViewText(R.id.NotoficationTitle,职称);
    contentView.setTextViewText(R.id.text,MESG);    notification.contentView =内容查看;    意图notificationIntent =新的Intent();
    notificationIntent = ActionIntent;    的PendingIntent mPendingIntent = PendingIntent.getActivity(mContext,0,
            notificationIntent,
            notification.flags | = Notification.FLAG_AUTO_CANCEL
                    | Notification.FLAG_SHOW_LIGHTS); // PendingIntent.FLAG_CANCEL_CURRENT);    //通未决意图内容意图通知意向
    notification.contentIntent = mPendingIntent;    //设置通知通知经理
    nManager.notify(0,通知);
}

5]在声明Manifest.xml文件接收器的名称

 <用途..
<接收机器人:名字=com.services.AlarmReceiver/>
< /用途>

我希望这充分code帮助你..谢谢..:)

I want to create a status bar notification which is displayed every 30 seconds even when the application is closed. I have searched and implemented many suggestions but none seems to work. I am able to create notification but when my application is not running, no notifications are displayed. I don't want the notifications to be displayed when my application is running. Below is my code for creating the notification:

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();
}

解决方案

I have mention full code of notification. Try this Code.

1] Declare Services start on splash Screen

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

2] Make ServiceManager class

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] Make 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] Make your custom notification function and "custom_notification.xml" file.

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] Declare receiver name on Manifest.xml file

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

I hope this full code help for you.. Thanks.. :)

这篇关于如何创建它,即使在应用程序关闭,每隔30秒显示状态栏通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆