如何从通知中打开当前片段?

时间:2015-02-27 06:36:07

标签: android

我制作了一个带故事的应用程序,其中一些tham会有文字和音频。 ol导航我在片段中制作,所以首先 - 片段与listView与taleNames,当一些选择 - 下一个片段,我将上传文本和音频选择的故事。对于音频,我正在使用服务,并且我在audioTales启动时也会发送通知。

我的问题是下一个:当我启动服务和音频开始播放时,我从我的应用程序隐藏到主屏幕,而不是点击工具栏中的通知。 我 需要通知将我推送到我现在正在播放音频Tale的当前片段,但事实上它将我推送到ListView

这是我的通知:

    private void initNotification(){
            CharSequence tikerText = getResources().getString(R.string.tickerText);
            NotificationManager mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
            Notification notification = new Notification(R.drawable.ic_launcher,tikerText,System.currentTimeMillis());
            notification.flags = Notification.FLAG_ONGOING_EVENT;
            Context context = getApplicationContext();
            CharSequence contentTitle = getResources().getString(R.string.contentTitle);
            CharSequence contentText = getResources().getString(R.string.contentText);
            Intent notifIntent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
            notifIntent.putExtra("showAudioFrag",true);
            PendingIntent contentIntent = PendingIntent.getActivity(context,0,notifIntent,0);
            notification.setLatestEventInfo(context,contentTitle,contentText,contentIntent);
            mNotificationManager.notify(NOTIFICATION_ID,notification);
        }

下一个代码iam在OnCreate的MainActivity中添加:

intent = getIntent();
        boolean reloadFragmentFromNotification = intent.getBooleanExtra("showAudioFrag",false);
        if (reloadFragmentFromNotification){
            Fragment fragment = new TaleActivity_Audio();
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(R.id.container,fragment)
                    .commit();
        } else {

            mNavigationDrawerFragment = (NavigationDrawerFragment)
                    getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
            mTitle = getTitle();

            // Set up the drawer.
            mNavigationDrawerFragment.setUp(
                    R.id.navigation_drawer,
                    (DrawerLayout) findViewById(R.id.drawer_layout));

        }

如果需要gitHub上的完整项目:https://github.com/radric/Tales_updated.git

1 个答案:

答案 0 :(得分:1)

您应该尝试启动包含活动的片段。并添加额外的婴儿车来检测哪个片段必须显示listview或audioview,

类似

Intent notificationIntent = new Intent(getApplicationContext(), viewmessage.class);
notificationIntent.putExtra("showAudioFrag", true);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity(getApplicationContext(),notificationIndex,notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(getApplicationContext(), notificationTitle, notificationMessage, pendingNotificationIntent);

在您的活动中

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);    
    onNewIntent(getIntent());
}

@Override
public void onNewIntent(Intent intent){
    Bundle extras = intent.getExtras();
    boolean reloadFragmentFromNotification = extras .getExtra("showAudioFrag",false);
    if (reloadFragmentFromNotification){
        Fragment fragment = new TaleActivity_Audio();
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.container,fragment)
                .commit();
    } else {

        mNavigationDrawerFragment = (NavigationDrawerFragment)
                getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
        mTitle = getTitle();

        // Set up the drawer.
        mNavigationDrawerFragment.setUp(
                R.id.navigation_drawer,
                (DrawerLayout) findViewById(R.id.drawer_layout));

    }
}

希望这会对你有所帮助,