Android 如何在特定时间显示通知

Android 如何在特定时间显示通知,android,Android,请告诉我可以在下面的代码中添加什么,以便在特定时间显示通知 public class MainActivity extends Activity { private WebView wv1; Context context; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanc

请告诉我可以在下面的代码中添加什么,以便在特定时间显示通知

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

        }

    }

我默认显示通知,但不是特定于时间的。请帮忙。我不知道如何添加AlarmManager或任何需要添加以显示通知的类。

您需要一个
AlarmManager

这并不复杂,这是一个参考:

这是一个实践:

或多或少你的代码我会这样写:

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.SetReAppeating
重复报警,直到执行
AlarmManager.cancel(PendingEvent)为止
否则,您可以使用它
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
add

<receiver android:name=".sendNotification"/>

应用程序中
标记


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

这是报警管理器的示例。从这里你可以实现你自己的逻辑&当警报触发时显示通知。我唯一的要求是在特定时间显示通知。因此,我可以设置一个条件,即达到某个时间,如果通知显示getSystemService上的设置错误,那么我如何设置setrepeating上的时间。就像特定的时间一样,代码是不正确的。c是上下文。如果这是错误,不要复制我的代码,而是尝试研究它。现在我解决了这个问题,我得到了c的东西,只是broadcastreceiver类中的getSystemService错误。这个错误在activity类中没有发生,但是在broadcast receiver类中发生了。您是否尝试过
context.getSystemService(context.NOTIFICATION\u SERVICE)