繁体   English   中英

以后如何显示状态栏通知?

[英]How to show status bar notification at a later time?

我已经成功创建了状态栏通知,但希望它在用户退出应用程序后6个小时弹出。

我有以下代码:

public class myClass extends superClass implements myinterface {

    final int NOTIF_ID = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {.........}

    /* more methods etc */ ......

    @Override
    protected void onDestroy() {
        View iop = (View) findViewById(R.id.app);
        sendNotification(iop);

        super.onDestroy();
    }

    public void sendNotification(View view) {

        // Use NotificationCompat.Builder to set up our notification.
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

        //icon appears in device notification bar and right hand corner of notification
        builder.setSmallIcon(R.drawable.ic_launcher);

        // This intent is fired when notification is clicked
        Intent intent = new Intent(view.getContext(), AndroidLauncher.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

        // Set the intent that will fire when the user taps the notification.
        builder.setContentIntent(pendingIntent);

        // Large icon appears on the left of the notification
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));

        // Content title, which appears in large type at the top of the notification
        builder.setContentTitle("Notifications Title");

        // Content text, which appears in smaller text below the title
        builder.setContentText("Your notification content here.");

        // The subtext, which appears under the text on newer devices.
        // This will show-up in the devices with Android 4.2 and above only
        builder.setSubText("Tap to view documentation about notifications.");

        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        // Will display the notification in the notification bar
        notificationManager.notify(NOTIF_ID, builder.build());
}

退出应用程序时会弹出状态栏通知,但是我希望自用户退出应用程序后的6个小时后弹出它。 我该怎么办?

提前致谢!

您可以使用AlarmManager安排包含您的通知的广播。

private void scheduleNotification() {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setContentTitle("Scheduled Notification");
    builder.setContentText(content);
    builder.setSmallIcon(R.drawable.ic_launcher);]
    Notification notification = builder.build();

    Intent notificationIntent = new Intent(this, NotificationPublisher.class);
    notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, 1);
    notificationIntent.putExtra(NotificationPublisher.NOTIFICATION, notification);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    long futureInMillis = System.currentTimeMillis() + TimeUnit.HOURS.toMillis(6);
    AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent);
}

然后使用BroadcastReceiver接收意图并显示通知。

public class NotificationPublisher extends BroadcastReceiver {

    public static String NOTIFICATION_ID = "notification-id";
    public static String NOTIFICATION = "notification";

    public void onReceive(Context context, Intent intent) {

        NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

        Notification notification = intent.getParcelableExtra(NOTIFICATION);
        int id = intent.getIntExtra(NOTIFICATION_ID, 0);
        notificationManager.notify(id, notification);

    }
}

不要忘记在您的AndroidManifest.xml文件中注册接收器。

(来源: https : //gist.github.com/BrandonSmith/6679223

创建一个新类,该类将使用未决意图和警报管理器执行警报。

long time= 6*60*60*1000; //6 hours
new Alarm_task(this, time).run();

public class Alarm_task implements Runnable{

    // The android system alarm manager
    private final AlarmManager am;
    // Your context to retrieve the alarm manager from
    private final Context context;
    long alarm_time;

    public Alarm_task(Context context, long time) {
        this.context = context;
        this.am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        this.alarm_time = time;
    }

    @Override
    public void run() {
        // Request to start are service when the alarm date is upon us
        //pop up a notification into the system bar not a full activity
        Intent i = new Intent("intent name");
        // can create a dialog in that intent or just call the sendNotification() function
        /** Creating a Pending Intent */
        PendingIntent operation = PendingIntent.getActivity(getBaseContext(), 0, i, Intent.FLAG_ACTIVITY_NEW_TASK);

        /** Setting an alarm, which invokes the operation at alart_time */
       alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,SystemClock.elapsedRealtime() + alarm_time, operation);
    }
}

在清单文件中定义意图:

<activity
        android:name=".Activity name"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="Intent name" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

现在,在该活动中,您可以在onCreate()期间调用Sendnotification()函数。或根据您的应用程序显示一些UI

onDestroy调用此方法

public void Remind (String title, String message)
    {



AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);

                        Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION");
                        notificationIntent .PutExtra ("message", message);
        notificationIntent .PutExtra ("title", title);
                    notificationIntent.addCategory("android.intent.category.DEFAULT");

                    PendingIntent broadcast = PendingIntent.getBroadcast(context, 0 , notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

                    Calendar cal = Calendar.getInstance();

                    if (android.os.Build.VERSION.SDK_INT>16)
                    {
                        alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis()+ 6*60*60*1000, broadcast);
                    }else
                    {
                        alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis() + 6*60*60*1000, broadcast);
                    }

}

创建一个新的JAVA文件

    public class Broadcast extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent1) {
        String message = intent1.getStringExtra ("message");
        String title = intent1.getStringExtra ("title");

// This intent is fired when notification is clicked
Intent notificationIntent = new Intent(context, AndroidLauncher.class);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addParentStack(AndroidLauncher.class);
    stackBuilder.addNextIntent(notificationIntent);

    PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);


// Use NotificationCompat.Builder to set up our notification.
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);

        //icon appears in device notification bar and right hand corner of notification
        builder.setSmallIcon(R.mipmap.ic_launcher);




        // Set the intent that will fire when the user taps the notification.
        builder.setContentIntent(pendingIntent);



        // Content title, which appears in large type at the top of the notification
        builder.setContentTitle("Notifications Title");

        // Content text, which appears in smaller text below the title
        builder.setContentText("Your notification content here.");

        // The subtext, which appears under the text on newer devices.
        // This will show-up in the devices with Android 4.2 and above only
        builder.setSubText("Tap to view documentation about notifications.");

        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        // Will display the notification in the notification bar
        notificationManager.notify(0, builder.build());



    }

在清单中注册此接收者

<receiver android:name=".Broadcast">
            <intent-filter>
                <action android:name="android.media.action.DISPLAY_NOTIFICATION" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>

您可以使用警报管理器服务和通知管理器来完成此操作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM