2016-12-14 72 views
1

我是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

创建一个单独的类来设置扩展BroadcastReceiver并从Fragment中调用它的报警。请参考链接: -

Alarm Manager Example

编辑: -

充气测试片段布局: -

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; 
    } 
+0

感谢你的答案,但为什么我需要一个报警?我只是想让测试片段类成为第一个实际显示的测试片段类在抽屉中。 – natsuVsnaruto

+0

我认为问题出现在这一行: - – Nainal

+0

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); – Nainal

相关问题