如何在特定时间显示通知

时间:2017-05-27 11:51:51

标签: android

请告诉我可以添加到以下代码中以显示特定时间的通知

public class MainActivity extends Activity {

        private WebView wv1;
        Context context;


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


            wv1 = (WebView) findViewById(R.id.YahooWhether);


            wv1.getSettings().setLoadsImagesAutomatically(true);
            wv1.getSettings().setJavaScriptEnabled(true);
            wv1.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
            wv1.setWebViewClient(new WebViewClient() {
                public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                    Toast.makeText(MainActivity.this, "here u go", Toast.LENGTH_SHORT).show();
                }
            });
            wv1.loadUrl("https://www.yahoo.com/news/weather/pakistan/sindh/karachi-2211096/");

            sendNotification();



        }
        public void sendNotification() {

    //Get an instance of NotificationManager//

            Notification.Builder mBuilder =
                    new Notification.Builder(this)
                            .setSmallIcon(R.mipmap.ic_launcher_round)
                            .setContentTitle("Whether Update")
                            .setContentText("Tomorrow's Forecast");


    // Gets an instance of the NotificationManager service//

            NotificationManager mNotificationManager =

                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);


            mNotificationManager.notify(001, mBuilder.build());

        }

    }

我已默认显示通知,但没有特定时间。请帮忙。我无法获得如何添加alarmanager或任何需要添加的类以显示通知。

1 个答案:

答案 0 :(得分:0)

您需要一个AlarmManager班级 这并不复杂,这是一个参考:
https://developer.android.com/reference/android/app/AlarmManager.html
这是一个实践:
https://developer.android.com/training/scheduling/alarms.html
或多或少你的代码我会这样写:

public class MainActivity extends Activity {

        private WebView wv1;
        Context context;
        AlarmManager alarmManager;
        Intent notificationAlert;


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


            wv1 = (WebView) findViewById(R.id.YahooWhether);


            wv1.getSettings().setLoadsImagesAutomatically(true);
            wv1.getSettings().setJavaScriptEnabled(true);
            wv1.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
            wv1.setWebViewClient(new WebViewClient() {
                public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                    Toast.makeText(MainActivity.this, "here u go", Toast.LENGTH_SHORT).show();
                }
            });
            wv1.loadUrl("https://www.yahoo.com/news/weather/pakistan/sindh/karachi-2211096/");

            alarmManager = ( AlarmManager ) context.getSystemService( Context.ALARM_SERVICE ); //request alarm service
            notificationAlert = new Intent( context, sendNotification.class ); //intent to connection at broadcastReceiver class
            PendingIntent pendingIntent = PendingIntent.getBroadcast( context, 0, notificationAlert, PendingIntent.FLAG_UPDATE_CURRENT ); //request broadcast in AlertNotification class
            try{
                alarmManager.setRepeating(
                        AlarmManager.RTC_WAKEUP, //alarm time in system
                        0, //start milliseconds
                        1000, //difference time to go
                        pendingIntent
                );
            }
            catch( ParseException e ){
                e.printStackTrace();
            }

        }

    }

我使用AlarmManager.setReapeating重复闹钟,直到你AlarmManager.cancel( pendingIntent );为止 否则你可以使用它AlarmManager.set

制作新课程通知:

public class sendNotification extends BroadcastReceiver {

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

    Drawable icon = context.getDrawable(R.mipmap.ic_launcher_round);

    //Get an instance of NotificationManager//

            NotificationCompat.Builder mBuilder =
                    new Notification.Builder(context)
                            .setSmallIcon(icon)
                            .setContentTitle("Whether Update")
                            .setContentText("Tomorrow's Forecast");


    // Gets an instance of the NotificationManager service//

            NotificationManager mNotificationManager =

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


            mNotificationManager.notify(001, mBuilder.build());

    }

        }

加入AndroidManifest.xml添加

<receiver android:name=".sendNotification"/>
application代码

中的

如果有任何问题,请告诉我。

相关问题