AlarmManger和通知无效

时间:2017-05-28 12:39:50

标签: java android alarmmanager

我尝试了一种在特定时间播放节目通知的方法,但仍无济于事。用于警报管理器的一个类通知一个,然后向清单添加通知

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);
        final TextView time = (TextView) findViewById(R.id.hello1);

        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/");

        Intent myIntent = new Intent(MainActivity.this , notify.class);
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        PendingIntent pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeZone(TimeZone.getTimeZone("Pakistan"));
        calendar.set(Calendar.HOUR_OF_DAY, 17);
        calendar.set(Calendar.MINUTE, 20);
        calendar.set(Calendar.SECOND, 00);

        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY , pendingIntent);

    }
}

这是通知类

public class notify extends BroadcastReceiver{
    Context context;
    @Override
    public void onReceive(Context context, Intent intent) {

        Notification.Builder mBuilder =
                new Notification.Builder(context)
                        .setSmallIcon(R.mipmap.ic_launcher_round)
                        .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());
    }
}

最后这是我的清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.inara.whetherapp">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver
            android:name=".notify"
            android:enabled="true"
            android:exported="true"></receiver>

    </application>
<uses-permission android:name="android.permission.INTERNET" />

0 个答案:

没有答案