两个App之间的传值最主要的是方法是

Intent intent = getPackageManager().getLaunchIntentForPackage("com.example.app2");
其他的传值用Intent完成就行 与activity之间的传值类似
(还可以自定义activity的权限)

APP1:
package com.example.app1;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btn1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn1= (Button) findViewById(R.id.btn_app1);
        btn1.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        // 通过包名获取要跳转的app,创建intent对象
        Intent intent = getPackageManager().getLaunchIntentForPackage("com.example.app2");

        if (intent != null) {
            // 这里跟Activity传递参数一样的嘛,不要担心怎么传递参数,还有接收参数也是跟Activity和Activity传参数一样
            intent.putExtra("value","我是App1里的值");
            startActivity(intent);
        } else {

            Toast.makeText(getApplicationContext(), "还没有这个APP", Toast.LENGTH_SHORT).show();
        }
    }
}

在App2中接收

package com.example.app2;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private TextView tv2;
    private Intent intent;
    private  String value;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         intent = getIntent();
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
             value = bundle.getString("value");
        }else{
            value="没有接收到App1的值";
        }

        tv2= (TextView) findViewById(R.id.tv2);
        tv2.setText(value);

    }
}

 

 
posted on 2015-12-14 16:54  毕哥  阅读(1503)  评论(0编辑  收藏  举报