Android在通知点击时打开特定标签片段

时间:2013-05-04 03:56:39

标签: android android-fragments android-actionbar android-notifications

我有一个使用Action Bar Tabs的android应用程序。还有一个通知系统。

我想在点击通知时直接打开特定标签。怎么做(因为通知挂起的意图只能打开活动,我的主要活动包含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);
    }

2 个答案:

答案 0 :(得分:4)

传递操作指定选项卡以及未决意图中的意图。在您的活动检索操作(请参阅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
}

答案 1 :(得分:0)

在此处找到我的答案:Launch a fragment in my Android application from the notification bar。我认为这与将extra Intent添加到您的Android有很大不同,但最好使用action的{​​{1}} - 想法。

将动作发送到您的通知功能:

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

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