gpt4 book ai didi

android - 通知不打开 fragment ?

转载 作者:搜寻专家 更新时间:2023-11-01 07:52:46 24 4
gpt4 key购买 nike

我创建了一个 Notification,我希望它在单击时打开一个 Fragment。它通常工作正常,但是当我当时在我的应用程序中打开另一个 fragment 时,它不起作用。

例如,我有两个 fragment :NoticiaFragEventoFrag。如果我打开我的应用程序并打开 NoticiaFrag 后按下设备的主页按钮最小化应用程序并收到 EventoFrag 的通知,当我单击通知时打开 NoticiaFrag 而不是 EventoFrag

为了完成这项工作,我需要使用后退按钮关闭应用程序。应用程序关闭后它工作正常。我想我需要在点击通知时重新打开应用程序,但我不知道如何。

从 Notification 打开 Fragment 的方法在 CustomDrawerLayout 中,调用了 openFrag()

我该如何解决这个问题?

通知

public class SendNotification {

public SendNotification(Context context, String title, String tickerText, String message, String url) {
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent;

Intent intent = new Intent(context, CustomDrawerLayout.class);

Bundle bd = new Bundle();
bd.putString("url", url);
intent.putExtras(bd);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

contentIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.logo)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setTicker(tickerText)
.setContentTitle(title)
.setShowWhen(true)
.setWhen(System.currentTimeMillis())
.setContentText(message);

mBuilder.setContentIntent(contentIntent);

Notification notification = mBuilder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.vibrate = new long[]{150, 300, 150, 600};
mNotificationManager.notify(AndroidSystemUtil.randInt(), notification);
}
}

list

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="br.com.ferpapps.santaluzapp" >

<!--Internet -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<!--GCM Permissions -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:name=".permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name=".permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.VIBRATE"></uses-permission>
<!---->

<!-- permissoes extra -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>


<application
android:name=".cv.CustomVolleySingleton"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
>

<!-- GCM -->
<receiver
android:name=".push.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="br.com.ferpapps" />
</intent-filter>
</receiver>
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<service
android:name=".push.GcmIntentService" >
</service>
<!-- close GCM -->


<activity
android:name=".act.SplashView"
android:label="@string/app_name"
android:windowSoftInputMode="adjustPan|adjustResize">

<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity android:name=".menu.CustomDrawerLayout"/>
<activity android:name=".act.AreaAlunoMainActivity" />

</application>
</manifest>

ActionBarActivity

public class CustomDrawerLayout extends ActionBarActivity implements OnItemClickListener{
private ActionBar ab;
private DrawerLayout dl;
private ListView lv;
private ActionBarDrawerToggle tg;
private LinearLayout navdrawer;

private List<ItensListView> fragments;
private CharSequence tl; //titulo principal
private CharSequence tlf; //titulo fragment

public static final String APP_NAME = "App";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_drawerlayout);
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.action_bar)));
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.actionbar_custom);

init();

openFrag();
}

private void openFrag(){
//verifica notificacao e abre o fragment correspondente
String url = getIntent().getStringExtra("url") != null ? getIntent().getStringExtra("url") : null;
Log.i("URL_NOTIFICACAO->", url != null ? url : "");
if(url != null) {
//Open By Notification
if (url.equals("Noticias")) {
selectedItem(0);
} else if (url.equals("Eventos")) {
selectedItem(1);
} else if (url.equals("Tarefas") ||
url.equals("Advertencias") ||
url.equals("Agendas")) {
Log.i("Notificacao", url);
Log.i("LOGADO NA SESSION", SessionUsuario.isLogged(this) + "");

if(SessionUsuario.isLogged(this)){
Log.i("Notificao Logado", "esta logado na area do aluno");
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
Fragment frag = AreaAlunoFrag.newInstance();
frag.getArguments().putString("url", url);
ft.replace(R.id.fl, frag);
ft.addToBackStack(APP_NAME);
ft.commit();
}
}
}else{
Log.i("ENTREI FIRST FRAG->","ENTREEIII");
//if(savedInstanceState == null){
selectedItem(0);
//}
}
}

private void init(){
//actionbar
onConfigActionBar();
//listview
configItensListView();
//drawerlayout
dl = (DrawerLayout)findViewById(R.id.dl);
navdrawer = (LinearLayout)findViewById(R.id.navdrawer);
//listview
lv = (ListView)findViewById(R.id.lv);
lv.setAdapter(new DrawerLayoutListViewAdapter(this, fragments));
lv.setOnItemClickListener(this);
//drawerlayout
//dl = (DrawerLayout)findViewById(R.id.dl);
//mDrawerRelativeLayout = (RelativeLayout)findViewById(R.id.left_drawer);
//actionbardrawertoggle
tg = new ActionBarDrawerToggle(this, dl, R.drawable.ic_launcher, R.string.drawer_open){
public void onDrawerClosed(View view) {
ab.setTitle(tl);
supportInvalidateOptionsMenu();
}

public void onDrawerOpened(View view) {
ab.setTitle(tlf);
supportInvalidateOptionsMenu();
}
};
dl.setDrawerListener(tg);
tl = tlf = getTitle();
}

/** ativa actionbar e botao home na action bar */
private void onConfigActionBar(){
ab = getSupportActionBar();
ab.setDisplayHomeAsUpEnabled(true);
ab.setHomeButtonEnabled(true);
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
tg.onConfigurationChanged(newConfig);
}

/** necessario */
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
tg.syncState();
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (tg.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}

/** necessario */
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.custom_drawer_layout, menu);
return true;
}

/** necessario */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
boolean status = dl.isDrawerOpen(navdrawer);
//menu.findItem(R.id.action_settings).setVisible(!status);
return super.onPrepareOptionsMenu(menu);
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
Log.i("POSITION->", position + "");
selectedItem(position);
}

/** seleciona o fragment q sera usado */
private void selectedItem(int position){
FragmentTransaction ft;
Fragment frag;
switch (position){
case 0:
ft = getSupportFragmentManager().beginTransaction();
frag = NoticiaFrag.newInstance();
ft.replace(R.id.fl, frag);
ft.addToBackStack(APP_NAME);
ft.commit();
break;
case 1:
ft = getSupportFragmentManager().beginTransaction();
frag = EventoFrag.newInstance();
ft.replace(R.id.fl, frag);
ft.addToBackStack(APP_NAME);
ft.commit();
break;
case 2:
ft = getSupportFragmentManager().beginTransaction();
frag = LoginFrag.newInstance();
ft.replace(R.id.fl, frag);
ft.addToBackStack(APP_NAME);
ft.commit();
break;
case 3:
ft = getSupportFragmentManager().beginTransaction();
frag = ContatoFrag.newInstance();
ft.replace(R.id.fl, frag);
ft.addToBackStack(APP_NAME);
ft.commit();
break;
case 4:
ft = getSupportFragmentManager().beginTransaction();
frag = CompartilhaFrag.newInstance();
ft.replace(R.id.fl, frag);
ft.addToBackStack(APP_NAME);
ft.commit();
break;
case 5:
ft = getSupportFragmentManager().beginTransaction();
frag = SobreFrag.newInstance();
ft.replace(R.id.fl, frag);
ft.addToBackStack(APP_NAME);
ft.commit();
break;
default:
closeApp();
break;
}
lv.setItemChecked(position, true);
setCustomTitle(fragments.get(position).getTexto());
dl.closeDrawer(navdrawer);
}

/** define o titulo da actionbar */
private void setCustomTitle(String title){
//SpannableString s = new SpannableString(title);
// s.setSpan(new TypefaceSpan(this, BatalhaConfigs.FONT), 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//ab.setTitle(s);
//tl = s;
}

@Override
public void onBackPressed() {
//if(getSupportFragmentManager().getBackStackEntryCount() > 0){
// getSupportFragmentManager().popBackStackImmediate();
// }else{
super.onBackPressed();
//}
}

@Override
protected void onResumeFragments() {
super.onResumeFragments();
}

/** Configura o List com as informacoes **/
private void configItensListView(){
fragments = new ArrayList<ItensListView>();
//define
ItensListView itens0 = new ItensListView("Noticias", R.drawable.setavermelha);
ItensListView itens1 = new ItensListView("Eventos", R.drawable.setavermelha);
ItensListView itens2 = new ItensListView("Área do Aluno", R.drawable.setavermelha);
ItensListView itens3 = new ItensListView("Contato", R.drawable.setavermelha);
ItensListView itens4 = new ItensListView("Redes Sociais", R.drawable.setavermelha);
ItensListView itens5 = new ItensListView("Sobre", R.drawable.setavermelha);
ItensListView itens6 = new ItensListView("Sair", R.drawable.setavermelha);

//add
fragments.add(itens0);
fragments.add(itens1);
fragments.add(itens2);
fragments.add(itens3);
fragments.add(itens4);
fragments.add(itens5);
fragments.add(itens6);

//AreaAlunoFrag
}

private void closeApp(){
System.exit(0);
}

@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
//super.onSaveInstanceState(outState, outPersistentState);
getSupportFragmentManager().beginTransaction().commitAllowingStateLoss();
}

private void removeAllFrags(){
FragmentManager fm = getSupportFragmentManager();
for(int x = 0; x < fm.getBackStackEntryCount(); x++){
//getSupportFragmentManager().beginTransaction().remove(fm.findFragmentById(x)).commit();
fm.popBackStackImmediate();
}
}

@Override
protected void onStart() {
new SendProjectId(getApplicationContext());
super.onStart();
}

@Override
protected void onResume() {
PushControl.setIsVisible(true);
new SendProjectId(getApplicationContext());
super.onResume();
}

@Override
protected void onPause() {
PushControl.setIsVisible(false);
super.onPause();
}

@Override
protected void onStop() {
PushControl.setIsVisible(false);
//removeAllFrags();
super.onStop();
}

@Override
protected void onDestroy() {
PushControl.setIsVisible(false);
super.onDestroy();
CustomVolleySingleton.getInstance().cancelPendingRequests(CustomVolleySingleton.TAG);
}

}

更新 CustomDrawerLayout 和 Manifest - 它工作正常。

自定义抽屉布局

public class CustomDrawerLayout extends ActionBarActivity implements OnItemClickListener{
private ActionBar ab;
private DrawerLayout dl;
private ListView lv;
private ActionBarDrawerToggle tg;
private LinearLayout navdrawer;

private List<ItensListView> fragments;
private CharSequence tl; //titulo principal
private CharSequence tlf; //titulo fragment

public static final String APP_NAME = "App";
private static String URL_NOTIFICATION = "";


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_drawerlayout);
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.action_bar)));
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.actionbar_custom);

init();

if(savedInstanceState == null){
selectedItem(0);
}
}

/** open fragment by notification */
private void openFragByNotification(){

if(!URL_NOTIFICATION.isEmpty()) {
//Open By Notification
if (URL_NOTIFICATION.equals("Noticias")) {
selectedItem(0);
} else if (URL_NOTIFICATION.equals("Eventos")) {
selectedItem(1);
} else if (URL_NOTIFICATION.equals("Tarefas") ||
URL_NOTIFICATION.equals("Advertencias") ||
URL_NOTIFICATION.equals("Agendas")) {
Log.i("Notificacao", URL_NOTIFICATION);
Log.i("LOGADO NA SESSION", SessionUsuario.isLogged(this) + "");

if(SessionUsuario.isLogged(this)){
Log.i("Notificao Logado", "esta logado na area do aluno");
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
Fragment frag = AreaAlunoFrag.newInstance();
frag.getArguments().putString("url", URL_NOTIFICATION);
ft.replace(R.id.fl, frag);
ft.addToBackStack(APP_NAME);
ft.commit();
}
}
}
}


private void init(){
//actionbar
onConfigActionBar();
//listview
configItensListView();
//drawerlayout
dl = (DrawerLayout)findViewById(R.id.dl);
navdrawer = (LinearLayout)findViewById(R.id.navdrawer);
//listview
lv = (ListView)findViewById(R.id.lv);
lv.setAdapter(new DrawerLayoutListViewAdapter(this, fragments));
lv.setOnItemClickListener(this);
//drawerlayout
//dl = (DrawerLayout)findViewById(R.id.dl);
//mDrawerRelativeLayout = (RelativeLayout)findViewById(R.id.left_drawer);
//actionbardrawertoggle
tg = new ActionBarDrawerToggle(this, dl, R.drawable.ic_launcher, R.string.drawer_open){
public void onDrawerClosed(View view) {
ab.setTitle(tl);
supportInvalidateOptionsMenu();
}

public void onDrawerOpened(View view) {
ab.setTitle(tlf);
supportInvalidateOptionsMenu();
}
};
dl.setDrawerListener(tg);
tl = tlf = getTitle();
}

/** ativa actionbar e botao home na action bar */
private void onConfigActionBar(){
ab = getSupportActionBar();
ab.setDisplayHomeAsUpEnabled(true);
ab.setHomeButtonEnabled(true);
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
tg.onConfigurationChanged(newConfig);
}

/** necessario */
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
tg.syncState();
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (tg.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}



/** necessario */
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.custom_drawer_layout, menu);
return true;
}


/** necessario */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
boolean status = dl.isDrawerOpen(navdrawer);
//menu.findItem(R.id.action_settings).setVisible(!status);
return super.onPrepareOptionsMenu(menu);
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
Log.i("POSITION->", position + "");
selectedItem(position);
}

/** seleciona o fragment q sera usado */
private void selectedItem(int position){
FragmentTransaction ft;
Fragment frag;
switch (position){
case 0:
ft = getSupportFragmentManager().beginTransaction();
frag = NoticiaFrag.newInstance();
ft.replace(R.id.fl, frag);
ft.addToBackStack(APP_NAME);
ft.commit();
break;
case 1:
ft = getSupportFragmentManager().beginTransaction();
frag = EventoFrag.newInstance();
ft.replace(R.id.fl, frag);
ft.addToBackStack(APP_NAME);
ft.commit();
break;
case 2:
ft = getSupportFragmentManager().beginTransaction();
frag = LoginFrag.newInstance();
ft.replace(R.id.fl, frag);
ft.addToBackStack(APP_NAME);
ft.commit();
break;
case 3:
ft = getSupportFragmentManager().beginTransaction();
frag = ContatoFrag.newInstance();
ft.replace(R.id.fl, frag);
ft.addToBackStack(APP_NAME);
ft.commit();
break;
case 4:
ft = getSupportFragmentManager().beginTransaction();
frag = CompartilhaFrag.newInstance();
ft.replace(R.id.fl, frag);
ft.addToBackStack(APP_NAME);
ft.commit();
break;
case 5:
ft = getSupportFragmentManager().beginTransaction();
frag = SobreFrag.newInstance();
ft.replace(R.id.fl, frag);
ft.addToBackStack(APP_NAME);
ft.commit();
break;
default:
closeApp();
break;

}

lv.setItemChecked(position, true);
setCustomTitle(fragments.get(position).getTexto());
dl.closeDrawer(navdrawer);
}

/** define o titulo da actionbar */
private void setCustomTitle(String title){
//SpannableString s = new SpannableString(title);
// s.setSpan(new TypefaceSpan(this, BatalhaConfigs.FONT), 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//ab.setTitle(s);
//tl = s;
}

@Override
public void onBackPressed() {
//if(getSupportFragmentManager().getBackStackEntryCount() > 0){
// getSupportFragmentManager().popBackStackImmediate();
// }else{
super.onBackPressed();

//}
}


@Override
protected void onResumeFragments() {
super.onResumeFragments();
}

/** Configura o List com as informacoes **/
private void configItensListView(){
fragments = new ArrayList<ItensListView>();
//define
ItensListView itens0 = new ItensListView("Noticias", R.drawable.setavermelha);
ItensListView itens1 = new ItensListView("Eventos", R.drawable.setavermelha);
ItensListView itens2 = new ItensListView("Área do Aluno", R.drawable.setavermelha);
ItensListView itens3 = new ItensListView("Contato", R.drawable.setavermelha);
ItensListView itens4 = new ItensListView("Redes Sociais", R.drawable.setavermelha);
ItensListView itens5 = new ItensListView("Sobre", R.drawable.setavermelha);
ItensListView itens6 = new ItensListView("Sair", R.drawable.setavermelha);

//add
fragments.add(itens0);
fragments.add(itens1);
fragments.add(itens2);
fragments.add(itens3);
fragments.add(itens4);
fragments.add(itens5);
fragments.add(itens6);


//AreaAlunoFrag

}

private void closeApp(){
System.exit(0);
}


@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
//super.onSaveInstanceState(outState, outPersistentState);
getSupportFragmentManager().beginTransaction().commitAllowingStateLoss();

}

private void removeAllFrags(){
FragmentManager fm = getSupportFragmentManager();
for(int x = 0; x < fm.getBackStackEntryCount(); x++){
//getSupportFragmentManager().beginTransaction().remove(fm.findFragmentById(x)).commit();
fm.popBackStackImmediate();
}
}


@Override
protected void onNewIntent(Intent intent) {
URL_NOTIFICATION = intent.getStringExtra("url");
Log.i("URL_NOTIFICATION", URL_NOTIFICATION);
super.onNewIntent(intent);
}

@Override
protected void onStart() {
new SendProjectId(getApplicationContext());
super.onStart();
}

@Override
protected void onResume() {
PushControl.setIsVisible(true);
new SendProjectId(getApplicationContext());
openFragByNotification();
super.onResume();
}

@Override
protected void onPause() {
PushControl.setIsVisible(false);
URL_NOTIFICATION = "";
super.onPause();
}

@Override
protected void onStop() {
PushControl.setIsVisible(false);
URL_NOTIFICATION = "";
super.onStop();
}

@Override
protected void onDestroy() {
PushControl.setIsVisible(false);
super.onDestroy();
CustomVolleySingleton.getInstance().cancelPendingRequests(CustomVolleySingleton.TAG);
}


}

list

<activity android:name=".menu.CustomDrawerLayout" android:launchMode="singleTop"/>

最佳答案

问题是 Android 默认情况下不会向已经运行的 Activity 传递新的 Intent。如果您通过后退按钮关闭您的应用程序,Activity 将被销毁,下次调用 startActivity() 时将创建一个新实例并创建新的 Intent 交付。如果您通过主页按钮离开 ActivityActivity 不会被销毁。当您在这种情况下调用 startActivity() 时,已经运行的 Activity 实例被带到前台,但是一个新的 Intent 是 < em>未交付。

要更改此行为,您可以使用 launchMode="singleTop" 在 list 中定义您的 Activity :

<activity
...
android:launchMode="singleTop"
...
>
</activity>

在这种情况下,Android 将通过onNewIntent() 传递一个新的Intent,您可以override :

This is called for activities that set launchMode to "singleTop" in their package, or if a client used the FLAG_ACTIVITY_SINGLE_TOP flag when calling startActivity(Intent). In either case, when the activity is re-launched while at the top of the activity stack instead of a new instance of the activity being started, onNewIntent() will be called on the existing instance with the Intent that was used to re-launch it.

刚刚注意到您传递给 Intent 的标志。在这种情况下,您可能只需要在 Activity 中覆盖 onNewIntent()

我自己也遇到过这个问题,问了一个问题here .该问题、已接受的答案和评论中有更多背景信息。

关于android - 通知不打开 fragment ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32553934/

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