特定时间错误的通知

时间:2015-09-01 14:07:44

标签: android xml permissions notifications

我正在尝试制作一个简单的应用,例如在特定时间发出通知(下午4:25)

但是当我的手机在下午4:25收到通知时

我不知道是什么问题

这就是代码

MainActivity.class

 package com.example.notifi;

 import android.app.Notification;
 import android.app.NotificationManager;
 import android.app.PendingIntent;
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
 import android.support.v4.app.NotificationCompat;
 import android.util.Log;

 public class Notificationmassage extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent arg1) {
    showNotification(context);
}

private void showNotification(Context context) {
    Log.i("notification", "visible");

    PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
            new Intent(context, Notificationmassage.class), 0);

    NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("xyz")
            .setContentText("It will contain dummy content");
    mBuilder.setContentIntent(contentIntent);
    mBuilder.setDefaults(Notification.DEFAULT_SOUND);
    mBuilder.setAutoCancel(true);
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(1, mBuilder.build());
}  
 }

activity_main.xml中

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

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="21" />

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

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver android:name="com.example.notifi.Notificationmassage"></receiver>
</application>

</manifest>

Notificationmassage.class

{{1}}

表现

{{1}}

这是我的所有代码

我不知道错误在哪里

请帮忙

谢谢

1 个答案:

答案 0 :(得分:0)

在这种情况下,您无法使用explicit intent,您必须使用implicit intent

new Intent(context, Notificationmassage.class)explicit intent

您应该将其更改为implicit intent,即new Intent("com.example.notifi.NOTIFI_RECEIVER")

您需要在清单

中定义BroadcastReceiver
<receiver android:name="com.example.notifi.Notificationmassage" >
    <intent-filter>
         <action android:name="com.example.notifi.NOTIFI_RECEIVER" />
    </intent-filter>
</receiver>