2016-12-14 132 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

相關問題