gpt4 book ai didi

android - 如何从通知中打开当前 fragment ?

转载 作者:太空狗 更新时间:2023-10-29 14:10:46 27 4
gpt4 key购买 nike

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

我的下一个问题是:当我启动服务并开始播放音频时,我从我的应用程序隐藏到主屏幕,而不是单击工具栏中的通知。我需要通知将我推送到正在播放音频 Tale 的当前 fragment ,但实际上它将我推送到 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);
}

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

最佳答案

您应该尝试启动包含 Activity 的 fragment 。并放置额外的婴儿车来检测哪个 fragment 必须显示 ListView 或音频 View ,

有点像

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

在你的 Activity 中

@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));

}
}

希望对你有帮助

关于android - 如何从通知中打开当前 fragment ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28758965/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com