安卓:在通知点击后打开特定选项卡碎片

5
我有一个使用Action Bar Tabs的安卓应用程序,还有一个通知系统。
我希望能够在点击通知时直接打开特定的标签页。但是因为通知PendingIntents只能打开活动,而我的主活动包含了3个片段用于3个标签页,所以该如何实现呢?
以下是标签页主活动的代码。
    public class MaintabActivity extends Activity {
public static Context appContext;
public static MapView mMapView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mainsc);
    appContext = getApplicationContext();

    startService(new Intent(this, MyService.class));


   //ActionBar
    ActionBar actionbar = getActionBar();
    actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    ActionBar.Tab ChatTab = actionbar.newTab().setText("Chat");
    ActionBar.Tab MapsTab = actionbar.newTab().setText("Maps");
    ActionBar.Tab SplashTab=actionbar.newTab().setText("Splash");

    Fragment ChatFrag = new ChatActivity();
    MapActivity mMapFragment = MapActivity.newInstance();
    Fragment SplashFrag = new SplashActivity();


    ChatTab.setTabListener(new MyTabsListener(ChatFrag));
    MapsTab.setTabListener(new MyTabsListener(mMapFragment));
    SplashTab.setTabListener(new MyTabsListener(SplashFrag));

    actionbar.addTab(ChatTab);
    actionbar.addTab(MapsTab);
    actionbar.addTab(SplashTab);

}




@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("tab", getActionBar().getSelectedNavigationIndex());
}

    }

    class MyTabsListener implements ActionBar.TabListener {
public Fragment fragment;

public MyTabsListener(Fragment fragment) {
    this.fragment = fragment;
}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
    Toast.makeText(MaintabActivity.appContext, "Reselected!", Toast.LENGTH_LONG).show();
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    ft.replace(R.id.fragment_container, fragment);
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    ft.remove(fragment);
}

    }

这是显示通知的服务代码。

    private void showNotification() {

        CharSequence text = getText(R.string.local_service_started);

        // Set the icon, scrolling text and timestamp
        Notification notification = new Notification(R.drawable.ic_launcher, text,
                System.currentTimeMillis());

        // The PendingIntent to launch our activity if the user selects this notification
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, MaintabActivity.class), 0);

        // Set the info for the views that show in the notification panel.
        notification.setLatestEventInfo(this, getText(R.string.local_service_label),
                       text, contentIntent);

        // Send the notification.
        mNM.notify(NOTIFICATION, notification);
    }

@user2319636-发布一些代码。 - TheFlash
1
代码是做什么的?它是常规操作栏选项卡,我想在点击通知时打开B选项卡。 - user2319636
当您点击通知时,它将调用包含选项卡的活动,并且您可以通过此方式在viewPager中调用特定的片段-- viewPager.setCurrentItem(position); - TheFlash
如果有两种不同类型的通知,我想打开不同的标签页,该怎么办?活动如何知道点击了哪个通知? - user2319636
@user2319636-你可以通过通知传递选项卡的位置...这样当调用活动时,它将根据视图页面中片段的位置显示...发布一些代码,以便我可以帮助你。 - TheFlash
显示剩余2条评论
2个回答

5

传递指定要打开的选项卡的操作,以及在待处理意图中的意图。然后在您的活动中检索传递的操作(请参见getIntent()),并根据它打开特定选项卡。

Intent intent = new Intent(this, Home.class);
intent.setAction("OPEN_TAB_1");
PendingIntent pending intent = PendingIntent.getService(this, 0, intent, 0);              

// In OnCreate() or depending on implementation  
if(getIntent().getAction().equals("OPEN_TAB_1") {
     // Open Tab
}

调用 ActionBar 的 selectTab() 方法。 - Anirudh
如果您正在使用 ActionBar(android.support.v7.app),请调用 actionBar.setSelectedNavigationItem(YOUR_TAB_ORDER_ID); - Arda Basoglu

0

在此处找到我的答案:如何从通知栏中启动Android应用程序中的片段。我认为这与向您的Intent添加extra并没有太大区别,但充分利用Androidaction思想是很好的。

将该操作发送到您的通知函数:

public static void notify(Context context, String message, String  action) {

    action = action.toUpperCase();

    //...

    Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());

    if(action != null && launchIntent != null){         
        launchIntent.setAction(action);         
    }

    PendingIntent pendingIntent = PendingIntent.getActivity(context, -1, launchIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    notification.when = System.currentTimeMillis();  
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 

    // Set the notification and register the pending intent to it
    notification.setLatestEventInfo(context, appName, message, pendingIntent);

    // Trigger the notification
    notificationManager.notify(0, notification);
}

然后获取操作并启动相应的片段。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接