【问题标题】:Displaying internal intent within a fragmentnot working在片段中显示内部意图不起作用
【发布时间】:2016-06-06 14:13:23
【问题描述】:

我有一个代码来调用意图来显示正在运行的服务屏幕。但我希望它显示在一个片段中。但我无法做到这一点。我从 java 类“bottompage.java”中调用这个意图,它是页面下半部分的片段。但是当按钮被点击时,这个意图仍然占据了全屏。

活动主要:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.app_display);
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.add(R.id.fragmentContainer1, new TopPage());
    fragmentTransaction.add(R.id.fragmentContainer2, new BottomPage());
    fragmentTransaction.commit();

    client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();

}

Bottompage.java

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View secondview = inflater.inflate(R.layout.bottom_fragment,container,false);
    Button dummy = (Button) secondview.findViewById(R.id.dummy);

    dummy.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v)
        {
            final String APP_DETAILS_PACKAGE_NAME ="com.android.settings";
            // Here you need to define the  package name

            final String SCREEN_CLASS_NAME ="com.android.settings.RunningServices";
            Intent servicesintent = new Intent();
            servicesintent.setAction(Intent.ACTION_VIEW);
            //intent.setAction(Intent.ACTION_VIEW);
            servicesintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            servicesintent.setClassName(APP_DETAILS_PACKAGE_NAME, SCREEN_CLASS_NAME);

            BottomPage.this.getActivity().startActivity(servicesintent);
        }
    });
    return secondview;
}

Main.xml 2个片段的xml文件

<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">

<TextView
    android:layout_width="157dp"
    android:layout_height="wrap_content"
    android:text="Second page"
    android:id="@+id/tv2"
    android:layout_gravity="center_horizontal"
    android:layout_weight="0.06" />

<FrameLayout
    android:id ="@+id/fragmentContainer1"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:layout_gravity="center_horizontal"
    android:layout_weight="0.06"></FrameLayout>

<FrameLayout
    android:id ="@+id/fragmentContainer2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerHorizontal="true"
    android:layout_below="@+id/fragmentContainer1"></FrameLayout>

底部片段的xml文件 BottomPage.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/bottom_fragment">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="call services"
    android:id="@+id/dummy"
    android:layout_gravity="center_horizontal" />

如何在 bottompage.java 中创建意图“servicesintent”以显示片段“fragmentcontainer2”中正在运行的服务。目前它正在全面屏。

【问题讨论】:

  • intent 是一个新活动,不能作为当前屏幕的一部分显示。

标签: android xml android-fragments android-studio android-intent


【解决方案1】:

我不确定你到底想要什么

我使用此代码在日志中显示激活我的 Fragment 的 Intent。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    ...
    Activity parent = this.getActivity();
    Intent intent = (parent == null) ? null : parent.getIntent();

    if (intent != null) {
        Log.d(Global.LOG_CONTEXT, "onCreateView " +
             intent.toUri(Intent.URI_INTENT_SCHEME));
    }

您可以将值设置为文本视图,而不是将其添加到日志中。

【讨论】:

  • HI...我想要通过调用意图来显示的屏幕 servicesintent.setClassName(APP_DETAILS_PACKAGE_NAME, SCREEN_CLASS_NAME); BottomPage.this.getActivity().startActivity(servicesintent);显示在mainxml的“fragment container2”区域内
  • Log.d(Global.LOG_CONTEXT, "dummy.OnClickListener " + servicesintent.toUri(Intent.URI_INTENT_SCHEME));
【解决方案2】:

如何在 bottompage.java 中创建意图“servicesintent”以在片段“fragmentcontainer2”中显示正在运行的服务。

你没有。您不能在自己的 UI 中嵌入第三方 UI,除非在非常特殊的情况下(例如,应用小部件和主屏幕)。

另请注意,不要求任何设备在导出的com.android.settings 中具有com.android.settings.RunningServices 活动。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多