BroadcastReceiver仅在打开应用程序时起作用

时间:2014-01-06 15:59:14

标签: java android xml broadcastreceiver android-manifest

我创建了一个书架的应用程序,因为我经常借书,我使用BroadcastReceiver在屏幕打开时发送通知。但是,BroadcastReceiver仅在应用程序打开时才起作用(通知在关闭时不会显示)。

MyReceiver.java

public class MyReceiver extends BroadcastReceiver
{
    public static final int ID_NOTFICATION_REMINDER = 0;
    SharedPreferences sp = MainActivity.sp;
    SharedPreferences.Editor edit = MainActivity.edit;
    private NotificationManager mNotificationManager;

    @SuppressWarnings("deprecation")
    public void onReceive(Context context, Intent intent)
    {
        if (intent.getAction().matches("android.intent.action.SCREEN_ON"))
        {
            for (int i = 0; i < sp.getInt("count", 0); i++)
            {
                mNotificationManager =         (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
                String name = sp.getString("name" + i, "NULL");
                String owners = sp.getString("owner" + i, "NULL");
                String holder = sp.getString("where" + i, "NULL");
                String date = sp.getString("date" + i, "NULL");
                SimpleDateFormat dfDate = new SimpleDateFormat(
                        "dd-MM-yyyy");
                Calendar c = Calendar.getInstance();
                String currentDate = dfDate.format(c.getTime());
                if ((!owners.equals(holder)) && (!name.isEmpty()) &&   (Utils.monthDiff(date, currentDate) >= 30))
                {
                    int icon = R.drawable.book_shelf_icon;
                    CharSequence tickerText = "A message from Book    Shelf";
                    long when = System.currentTimeMillis();
                    final Notification mNotification = new Notification(icon, tickerText, when);
                    String contentTitle = "The book "+name+" hasn't changed it's state for over a month!";
                    String contentText = "Tap to open Book Shelf";
                    Intent mIntent = new Intent(context, MainActivity.class);
                    PendingIntent mPendingIntent = PendingIntent.getActivity(context, 0, mIntent, 0);
                    mNotification.setLatestEventInfo(context, contentTitle, contentText, mPendingIntent);
                  mNotificationManager.notify(ID_NOTFICATION_REMINDER, mNotification);
                }
            }
        }    
    }
}

的AndroidManifest.xml

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/book_shelf_icon"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.asaf.applications.bookshelf.MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <receiver
            android:name="com.asaf.applications.bookshelf.MyReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.SCREEN_ON" />
            </intent-filter>
        </receiver>
    </application>

 </manifest>

1 个答案:

答案 0 :(得分:0)

您正在从MainActivity

获得对SharedPreferences的引用
  

SharedPreferences sp = MainActivity.sp;

但是,当活动未运行时,您将获得NPE(sp = null)

Broadacast接收器已经可以访问上下文,因此使用它来确定对SharedPreferences的引用

@SuppressWarnings("deprecation")
public void onReceive(Context context, Intent intent)
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); 
    /*
    * Your code
    */
}