将一个活动应用程序转换为两个片段应用程序

时间:2014-06-12 12:46:42

标签: android android-activity fragment

我是Android编程的初学者,最近我正在开发一个应用程序,它显示所有用户安装的应用程序具有可能危险的权限。

到目前为止,我的应用程序将所有风险应用程序显示在一个活动中,我想实现一个ActionBar选项卡布局,以便在可怕应用程序和安全应用程序之间滑动。

使用Tabs布局实现ActionBar不是问题,我已经成功完成了。 我现在想做的是将我的One Activity Permission App转换为Two Tab Fragment应用程序,我目前发现这样做有问题。

权限应用 这是我的Permission App java代码

    package com.permisisonanalyzer.permissionanalyzer;

import android.app.Activity;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.content.pm.PackageManager;

import java.util.ArrayList;
import java.util.List;


public class Permission_view extends Activity {

    String[] badPermissions = new String[] {"android.permission.INTERNET","android.permission.AUTHENTICATE_ACCOUNTS",
                                "android.permission.READ_CALL_LOG", "android.permission.READ_LOGS", "android.permission.READ_CONTACTS",
                                "android.permission.WRITE_SECURE_SETTINGS", "android.permission.PROCESS_OUTGOING_CALLS",
                                "android.permission.SEND_SMS", "android.permission.READ_SOCIAL_STREAM"};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_permission_view);
        // Definitions
        PackageManager pm = getPackageManager();
        ArrayList<String> valori = new ArrayList<String>();
        ListView listview = (ListView) findViewById(R.id.listview);

        // Retrieve all applications installed by user
        int flags = PackageManager.GET_META_DATA |
                    PackageManager.GET_SHARED_LIBRARY_FILES |
                    PackageManager.GET_UNINSTALLED_PACKAGES;
        List<ApplicationInfo> installed_packages = pm.getInstalledApplications(flags);

        for ( ApplicationInfo appInfo : installed_packages ) {
            // Select only the application installed by user
            if ( (appInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0 ) {
                // check if possess a dangerous permission
                try {
                    PackageInfo pkgInfo = pm.getPackageInfo(appInfo.packageName, PackageManager.GET_PERMISSIONS);
                    String[] requested_permission = pkgInfo.requestedPermissions;
                    if ( dangerous_permission(requested_permission)) {
                        // found an application with dangerous permission
                        valori.add( appInfo.loadLabel(pm).toString() );     // add application name in valori
                    }
                } catch (PackageManager.NameNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }

        // Create the display of the list throught a standard adapter
        ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, valori);
        listview.setAdapter(adapter);
    }

    private boolean dangerous_permission(String[] req_permissions) {
        boolean found = false;

        if ( req_permissions != null ) {
            for (int i = 0; i < req_permissions.length && !found; i++) {
                for (int j = 0; j < badPermissions.length && !found; j++) {
                    if (req_permissions[i].equals(badPermissions[j])) {
                        found = true;
                    }
                }
            }
        }

        return found;
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.permission_view, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

这是我的Permission App的XML文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".Permission_view">

    <TextView
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView"
        android:layout_centerHorizontal="true"
        android:textStyle="bold"
        android:textSize="25dp" />

    <ListView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/listview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

</RelativeLayout>

TabExampleApp

这是我的TabExampleApp java文件

package com.tabexample.app;

import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;


public class TabExample extends FragmentActivity implements ActionBar.TabListener {

    ViewPager viewPager;
    ActionBar actionBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab_example);

        // get the ViewPager handle
        viewPager = (ViewPager) findViewById(R.id.pager);
        viewPager.setAdapter(new MyPageAdapter(getSupportFragmentManager()));
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                actionBar.setSelectedNavigationItem(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        // create the actionBar and sets navigation mode to TABS
        actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // create TAB 1
        ActionBar.Tab list_tab = actionBar.newTab();
        list_tab.setText("Event List");
        list_tab.setTabListener(this);

        // create TAB 2
        ActionBar.Tab map_tab = actionBar.newTab();
        map_tab.setText("Event Map");
        map_tab.setTabListener(this);

        // add TABS to actionBar
        actionBar.addTab(list_tab);
        actionBar.addTab(map_tab);
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    }
}

class MyPageAdapter extends FragmentPagerAdapter {

    MyPageAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        Fragment fragment = null;
        switch (position) {
            case 0: fragment = new EventListFragment();
                break;
            case 1: fragment = new EventMapFragment();
                break;
        }
        return fragment;
    }

    @Override
    public int getCount() {
        return 2;
    }
}

这是我的Fragment1.java

package com.tabexample.app;



import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;



/**
 * A simple {@link Fragment} subclass.
 *
 */
public class EventMapFragment extends Fragment {


    public EventMapFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_event_map, container, false);
    }


}

片段2类似于片段1,我不会发布它

This is the SwypeTabApp XML

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".TabExample">

    <TextView
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</android.support.v4.view.ViewPager>

这是Fragment1 XML文件

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.tabexample.app.EventMapFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="event map" />

</FrameLayout>

我很难理解的是,将与Application Installed(最初置于PermissionApp活动的onCreate()中)的信息检索相关的代码放到Fragment中。 我试图将它放入onActivityCreated()但它崩溃了,并将它放入Framework1的onCreate()。

很抱歉可能缺少信息,我只是Android编程的初学者

0 个答案:

没有答案