我正在使用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,通知我通知的标题,但我看不到视图中的值..通知的布局是空的..怎么可能?
或者我如何将活动中的值放入我的片段?
答案 0 :(得分:0)
当您为布局充气时,它会创建一个由新对象组成的新树。
完成您尝试的操作的方法是缓存您在onCreate
中更改的所有视图,并在BroadcastReceiver的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);
}