【问题标题】:Android can't show notificationAndroid 无法显示通知
【发布时间】:2015-07-02 23:29:35
【问题描述】:

我正在使用 NotificationService 和 Broadcastreceiver 来获取传入的通知。服务从清单开始:

<service android:name="com.myapp.notifications.MyNotificationListenerService"
            android:label="@string/app_name"
            android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">

            <intent-filter>

                <action android:name="android.service.notification.NotificationListenerService" />

            </intent-filter>

        </service>

就是这个;

public class MyNotificationListenerService extends NotificationListenerService{
    Context context;

    @Override

    public void onCreate() {

        super.onCreate();
        context = getApplicationContext();

    }
    @Override

    public void onNotificationPosted(StatusBarNotification sbn) {


        String pack = sbn.getPackageName();
        String ticker = sbn.getNotification().tickerText.toString();
        Bundle extras = sbn.getNotification().extras;
        String title = extras.getString("android.title");
        String text = extras.getCharSequence("android.text").toString();

        Log.i("Package",pack);
        Log.i("Ticker",ticker);
        Log.i("Title",title);
        Log.i("Text",text);

        Intent msgrcv = new Intent("Msg");
        msgrcv.putExtra("package", pack);
        msgrcv.putExtra("ticker", ticker);
        msgrcv.putExtra("title", title);
        msgrcv.putExtra("text", text);

        LocalBroadcastManager.getInstance(context).sendBroadcast(msgrcv);


    }

    @Override

    public void onNotificationRemoved(StatusBarNotification sbn) {
        Log.i("Msg","Notification Removed");

    }
}

当通知到达时,日志会正确显示标题、包裹和其他信息。问题是这个;

我有一个 Activity,它是 MainActivity,我在其中有 NavigationDrawer。我要显示传入通知的类是片段!所以我在 MainActivity 中创建了 Broadcastreceiver,然后我用 LayoutInflater 填充片段的布局,以通过这种方式获取我需要的所有组件:

public BroadcastReceiver onNotice= new BroadcastReceiver() {
        LinearLayout notificationLayout;
        Drawable icon;

        @Override
        public void onReceive(Context context, Intent intent) {
            final String pack = intent.getStringExtra("package");
            String title = intent.getStringExtra("title");
            String text = intent.getStringExtra("text");

            LayoutInflater mInf = LayoutInflater.from(context);
            View myView = mInf.inflate(R.layout.activity_main, null);
            notificationLayout = (LinearLayout)myView.findViewById(R.id.notificationLayout);
            TextView notificationDescription = (TextView) myView.findViewById(R.id.notificationDesc);
            TextView notificationTitle = (TextView) myView.findViewById(R.id.notificationTitle);
            CircularImageView notificationImage = (CircularImageView) myView.findViewById(R.id.img_thumbnail);

            Toast.makeText(DrawerActivity.this, title, Toast.LENGTH_SHORT).show();

            if(!pack.equals("") || !title.equals("") || !text.equals("")) {
                notificationLayout.setVisibility(View.VISIBLE);
                notificationTitle.setText(title);
                notificationDescription.setText(text);

                try {
                    icon = DrawerActivity.this.getPackageManager().getApplicationIcon(pack);
                } catch (PackageManager.NameNotFoundException e) {
                    e.printStackTrace();
                }
                notificationImage.setImageDrawable(icon);

            } else {
                notificationLayout.setVisibility(View.INVISIBLE);
            }

        }
    };

当通知到达时,现在出现通知我通知标题的 Toast,但我在视图中看不到值。通知的布局是空的。怎么可能?

或者我如何将活动中的值放入我的片段?

【问题讨论】:

  • 只是为了解决问题。当您将膨胀布局添加到片段根视图时?

标签: java android android-fragments notifications broadcastreceiver


【解决方案1】:

当您扩充布局时,它会创建一个由新对象组成的新树。

完成您尝试的方法是缓存您将在onCreate 中更改的任何视图,并在您的广播接收器的onReceive 中引用它们。

private TextView textView;
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        //TODO: Update Views
    }
};

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View root = inflater.inflate(R.layout.my_fragment, container, false);

    //Cache views
    textView = (TextView) root.findViewById(R.id.textview);

    return root;
}

@Override
public void onResume() {
    super.onResume();

    getActivity().registerReceiver(broadcastReceiver, filter);
}

@Override
public void onPause() {
    super.onPause();

    getActivity().unregisterReceiver(broadcastReceiver);
}

【讨论】:

  • mm,如果我按照您的建议,我现在如何更改我的代码?可以给我看看吗?
  • 你的fragment是否总是会在activity中持久化?什么时候添加?
  • 我在 navdrawer 的 onNavigationItemSelected 中调用它:setNewRootFragment(Fragment.newInstance()); 我只是想办法将值放在那里..或者如果有办法将 Breadcastreceiver 直接放在 Fragment 中..我也尝试过这种方式,但 Br 没有启动..
  • 为什么不呢?你在哪里注册的?我认为最好在onResumeonPause 中处理它。
  • 我试过 onResume()..你想在片段中如何调用它?
猜你喜欢
  • 2018-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-09
  • 1970-01-01
  • 2019-02-24
  • 1970-01-01
相关资源
最近更新 更多