0

我在我的应用程序中显示一个运行正常的自定义通知,但我想在设备启动后启动此通知,但此时它正在生成错误

错误日志:

01-17 20:29:40.281  1652  1785 W PackageManager: Failure retrieving resources for com.ltlgen.guest_mode: Resource ID #0x0

01-17 20:30:42.721  1070  1629 D ActivityManager: startProcessLocked calleePkgName: com.ltlgen.guest_mode, hostingType: broadcast-2

01-17 20:30:42.781  1070  1629 I ActivityManager: Start proc 6558:com.ltlgen.guest_mode/u0a188 for broadcast-2 com.ltlgen.guest_mode/.BootReceiver

01-17 20:30:42.971  6558  6558 W ResourcesManager: getTopLevelResources: com.ltlgen.guest_mode for user  0

01-17 20:30:42.991  6558  6558 D InjectionManager: fillFeatureStoreMap com.ltlgen.guest_mode

01-17 20:30:43.001  6558  6558 I InjectionManager: Constructor com.ltlgen.guest_mode, Feature store :{}

01-17 20:30:43.001  6558  6558 D guest_mode.BootReceiver: android.intent.action.BOOT_COMPLETED

01-17 20:30:43.091  6558  6558 E AndroidRuntime: Process: com.ltlgen.guest_mode, PID: 6558

01-17 20:30:43.091  6558  6558 E AndroidRuntime: java.lang.RuntimeException: Unable to start receiver com.ltlgen.guest_mode.BootReceiver: java.lang.IllegalArgumentException: No such package 

01-17 20:30:43.091  6558  6558 E AndroidRuntime:    at com.ltlgen.guest_mode.MainActivity.handleNotification(MainActivity.java:149)

01-17 20:30:43.091  6558  6558 E AndroidRuntime:    at com.ltlgen.guest_mode.BootReceiver.onReceive(BootReceiver.java:16)

01-17 20:30:43.291  1070  1102 D StatusBarManagerService: manageDisableList userId=0 what=0x0 pkg=Window{4ea5511 u0 Application Error: com.ltlgen.guest_mode}

01-17 20:30:45.331  1070  1692 I ActivityManager: Process com.ltlgen.guest_mode (pid 6558)(adj 15) has died(34,451)

错误:第 149 行没有这样的包

这是第 149 行

RemoteViews view = new RemoteViews(getPackageName(), R.layout.notification);

完整的方法代码如下所示

public void handleNotification()
{
    int mId = 22120927;

    boolean active = isNotificationActive();
    PendingIntent pi = PendingIntent.getService(mContext, 0, new Intent(mContext, MainService.class), PendingIntent.FLAG_CANCEL_CURRENT);

    NotificationCompat.Builder n = new NotificationCompat.Builder(mContext);
    n.setOngoing(true)
        .setSmallIcon(R.drawable.ic_launcher);
    if (DEBUG_MODE)
    {
        n.setPriority(NotificationCompat.PRIORITY_MIN);
    }

    RemoteViews view = new RemoteViews(getPackageName(), R.layout.notification);
    view.setOnClickPendingIntent(R.id.notificationImageButton1, pi);
    n.setContent(view);

    NotificationManager mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);

    if (active)
    {
        mNotificationManager.notify(mId, n.build());
    }
    else
    {
        mNotificationManager.cancel(mId);
    }
}

引导接收器.xml

MainActivity main = new MainActivity();
    main.handleNotification();
    main.finish();

AndroidManifest.xnl

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"
    android:resizeableActivity = "true">
    <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> 

    <activity
        android:name=".DialerActivity"
        android:label=""
        android:windowSoftInputMode="stateVisible" />

    <service android:name=".MainService" />

    <receiver
        android:name=".BootReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
        </intent-filter>
    </receiver>

</application>

4

1 回答 1

0

我的问题在@Mike M 的评论的帮助下解决了

GetPackageManager() 需要从 BootReceiver 手动创建 MainActivity 类实例时未初始化的上下文

现在,我在 MyNotificationManager 类中分离了通知处理,如下所示:

public class MyNotificationManager
{
private Context mContext;

SharedPreferences mShPrefs;
SharedPreferences.Editor mShPrefsEditor;
private static final String KEY_NOTIFICATION_STATUS = "notification_status";

public MyNotificationManager(Context context)
{
    mContext = context;

    mShPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
    mShPrefsEditor = mShPrefs.edit();
}

public void handleNotification()
{
    int mId = 22120927;

    boolean active = isNotificationActive();
    PendingIntent pi = PendingIntent.getService(mContext, 0, new Intent(mContext, MainService.class), PendingIntent.FLAG_CANCEL_CURRENT);

    NotificationCompat.Builder n = new NotificationCompat.Builder(mContext);
    n.setOngoing(true)
        .setSmallIcon(R.drawable.ic_launcher);
    if (MainActivity.DEBUG_MODE)
    {
        n.setPriority(NotificationCompat.PRIORITY_MIN);
    }

    RemoteViews view = new RemoteViews(mContext.getPackageName(), R.layout.notification);
    view.setOnClickPendingIntent(R.id.notificationImageButton1, pi);
    n.setContent(view);

    NotificationManager mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);

    if (active)
    {
        mNotificationManager.notify(mId, n.build());
    }
    else
    {
        mNotificationManager.cancel(mId);
    }
}

}

并在 BootReceiver onReceive()

public void onReceive(Context context, Intent intent)
{
    Log.d(TAG, intent.getAction().toString());

    MyNotificationManager myNotificationMan = new MyNotificationManager(context);
    myNotificationMan.handleNotification();
}
于 2018-01-18T04:51:16.593 回答