Android ViewPager问题

时间:2017-05-15 08:47:06

标签: java android android-viewpager

我正在开发android Launcher,其中包含三个活动,第一个活动是HomeActivity,其中包含四个按钮,这些按钮将执行意图操作,其余活动为空白但是,{{1}当我点击联系人按钮时它会显示联系人列表,但当我选择其中一个联系人时它没有显示任何内容,其余的按钮也表现得像联系人按钮,它不会跳转到HomeActivity方法所以怎么能我解决了这个问题?

以下是我的代码: -

MainHomeActivity.java

OnActivityResult()

HomeActivity.java

public class MainHomeActivity extends AppCompatActivity {

    DevicePolicyManager devicePolicyManager;
    ComponentName adminComponent;
    Button apps;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        Button lock = (Button) findViewById(R.id.button_lock);
        lock.setOnClickListener(btnListener);
        Settings.System.putInt(getContentResolver(),Settings.System.SCREEN_OFF_TIMEOUT,15000);

        apps = (Button) findViewById(R.id.main_menu);
    }

    Button.OnClickListener btnListener = new Button.OnClickListener() {
        @Override
        public void onClick(View v) {
            adminComponent = new ComponentName(MainHomeActivity.this, AdminReceiver.class);
            devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);

            if (!devicePolicyManager.isAdminActive(adminComponent)) {

                Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
                intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, adminComponent);
                startActivityForResult(intent, 50);
            } else {
                devicePolicyManager.lockNow();
            }

            apps.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent i = new Intent(MainHomeActivity.this, AppsListActivity.class);
                    startActivity(i);
                }
            });
        }
    };
}

CustomPagerAdapter.java

public class HomeActivity extends Activity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_home_activity);

        ViewPager viewPage = (ViewPager) findViewById(R.id.viewpager);
        viewPage.setAdapter(new CustomPageAdapter(this));
    }

    public void contact(View view){
        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
        if(intent.resolveActivity(getPackageManager()) != null){
            startActivityForResult(intent,10);
        }
    }


    public void audioIntent(View view){
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getParent()+"/ext_card");
        intent.setDataAndType(uri,"audio/*");
        startActivityForResult(intent,20);
    }

    public void videoIntent(View v){
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getParent()+"/ext_card");
        intent.setDataAndType(uri,"video/*");
        startActivityForResult(intent,30);
    }

    public void pdfIntent(View view){
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getParent()+"/ext.card");
        intent.setDataAndType(uri,"pdf/*");
        startActivityForResult(intent,40);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == RESULT_OK && resultCode == 10){
            Uri contactUri = data.getData();
            Intent contactIntent = new Intent(Intent.ACTION_CALL);
            contactIntent.setData(contactUri);
            startActivity(contactIntent);
        }

        if(resultCode == 20 && requestCode == RESULT_OK){
            Uri audioUri = data.getData();
            Intent audioIntent = new Intent(Intent.ACTION_VIEW);
            audioIntent.setDataAndType(audioUri,"audio/*");
            startActivity(audioIntent);
        }

        if(requestCode == RESULT_OK && resultCode == 30){
            Uri videoUri = data.getData();
            Intent videoIntent = new Intent(Intent.ACTION_VIEW);
            videoIntent.setDataAndType(videoUri,"video/*");
            startActivity(videoIntent);
        }

        if(requestCode ==  RESULT_OK && resultCode == 40){
            Uri pdfUri = data.getData();
            Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
            pdfIntent.setDataAndType(pdfUri,"pdf/*");
            startActivity(pdfIntent);
        }

    }
}

这是该应用程序的屏幕截图: -

enter image description here

1 个答案:

答案 0 :(得分:1)

你在if条件下做错了。您正尝试将requestCodeRESULT_OKresultCode与请求代码(10,20,30 and 40)进行比较,这是完全错误的。

使用:

if(requestCode == 10 && resultCode == RESULT_OK)

代替:

if(requestCode == RESULT_OK && resultCode == 10)

更新onActivityResult(),如下所示:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode == 10 && resultCode == RESULT_OK){
        Uri contactUri = data.getData();
        Intent contactIntent = new Intent(Intent.ACTION_CALL);
        contactIntent.setData(contactUri);
        startActivity(contactIntent);
    }

    if(requestCode == 20 && resultCode == RESULT_OK){
        Uri audioUri = data.getData();
        Intent audioIntent = new Intent(Intent.ACTION_VIEW);
        audioIntent.setDataAndType(audioUri,"audio/*");
        startActivity(audioIntent);
    }

    if(requestCode == 30 && resultCode == RESULT_OK){
        Uri videoUri = data.getData();
        Intent videoIntent = new Intent(Intent.ACTION_VIEW);
        videoIntent.setDataAndType(videoUri,"video/*");
        startActivity(videoIntent);
    }

    if(requestCode == 40 && resultCode == RESULT_OK){
        Uri pdfUri = data.getData();
        Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
        pdfIntent.setDataAndType(pdfUri,"pdf/*");
        startActivity(pdfIntent);
    }

}

希望这会有所帮助〜