我是android的新开发者。我在片段和通知方面遇到困难。这是我的一些代码。
test.java
public class test extends Fragment {
\\some of codes here up.
@Override
public View onCreateView(LayoutInflater, ViewGroup container, Bundle savedIntancesState) {
View view = inflater.inflate(R.layout.test_fragment, container, false);
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
PendingIntent resultPendingIntent = PendingIntent.getActivity(getActivity(), fragmentManager.beginTransaction().replace(R.id.flContent, NextFragment.newInstance("message", R.layout.content_row)).commit(), getActivity().getIntent(), PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new NotificationCompat.Builder(getActivity())
.setContentTitle(title)
.setContentText(message)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(resultPendingIntent)
.setSound(soundUri)
.addAction(R.mipmap.ic_launcher, "Call",resultPendingIntent).build();
NotificationManager notificationManager = (NotificationManager) getActivity().getSystemService(NOTIFICATION_SERVICE);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0,notification);
return view;
}
我希望首先显示测试片段并发出通知,但该代码的结果是始终显示NextFragment类。请告诉我哪里出错了。我感谢任何帮助。非常感谢你。
答案 0 :(得分:0)
创建一个单独的类来设置警报,该类扩展了BroadcastReceiver并从Fragment中调用它。请参阅链接: -
编辑: -
夸大您的测试片段布局: -
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.test_fragment, container, false);
//your code for notification
return view;
}