首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Remoteview的自定义通知在启动后不显示

使用Remoteview的自定义通知在启动后不显示
EN

Stack Overflow用户
提问于 2018-01-17 23:36:06
回答 1查看 1.4K关注 0票数 0

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

错误日志目录:

代码语言:javascript
复制
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行

代码语言:javascript
复制
RemoteViews view = new RemoteViews(getPackageName(), R.layout.notification);

完整的方法代码如下所示

代码语言:javascript
复制
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);
    }
}

BootReceiver.xml

代码语言:javascript
复制
MainActivity main = new MainActivity();
    main.handleNotification();
    main.finish();

AndroidManifest.xnl

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>

代码语言:javascript
复制
<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>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-18 12:51:17

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

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

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

代码语言:javascript
复制
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()中

代码语言:javascript
复制
public void onReceive(Context context, Intent intent)
{
    Log.d(TAG, intent.getAction().toString());

    MyNotificationManager myNotificationMan = new MyNotificationManager(context);
    myNotificationMan.handleNotification();
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48304741

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档