[android] 11_利用intent在不同intent之间进行数据传递-人品计算器

Android 4.0

Intent putExtra(String name, Serializable value)  
Intent putExtra(String name, Parcelable value)   

// 4、将数据传递给另外一个Activity,利用隐式意图传递过去,带数据
Intent intent = new Intent();
intent.setAction("cn.zengfansheng.result.rp");
intent.putExtra("name", name);
intent.putExtra("total", total);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(intent);

2、取数据
// 获取将当前Activity激活的Intent
Intent intent = this.getIntent();
String name = intent.getStringExtra("name");
int total = intent.getIntExtra("total", 50);  

3、如果startActivity()不是Activity,而是Context,则需要加上下面代码
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(intent);
android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want? 人品计算器 
主Activity: 
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends Activity {
    private EditText et_name;
    private ProgressBar pb;
    private TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        et_name = (EditText) this.findViewById(R.id.et_name);
        pb = (ProgressBar) this.findViewById(R.id.pb);
        tv = (TextView) this.findViewById(R.id.tv);
    }
    /**
     * 计算人品
     * @param view
     */
    public void rpcalc(View view) {
        
        // 0、一计算就将tv设置为可见
        tv.setVisibility(View.VISIBLE);
        pb.setVisibility(View.VISIBLE);
        new Thread(){
            @Override
            public void run() {
                // 1、获取用户输入的名字
                String name = et_name.getText().toString();
                // 2、将名字转成byte数组,转成int,然后将其加起来,total作为总分
                byte[] bytes = name.getBytes();
                // 3、设置pb最大值
                pb.setMax(bytes.length - 1);// 防止进度条还没有走完结果就出来了
                int total = 0;// rp总分
                for (int i = 0; i < bytes.length; i++) {
                    pb.setProgress(i);
                    total += (int) Math.abs(bytes[i]);// 由于转成字节,汉字会有负数,取绝对值
                    try {
                        Thread.sleep(300);// 每计算一次,睡0.5秒钟。
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                total = total % 100;// 如果名字过长的,取余,满分为100分
                // 4、将数据传递给另外一个Activity,利用隐式意图传递过去,带数据
                Intent intent = new Intent();
                intent.setAction("cn.zengfansheng.result.rp");
                intent.putExtra("name", name);
                intent.putExtra("total", total);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                getApplicationContext().startActivity(intent);
            }
        }.start();
    }
}  
激活的Activity:
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;
import cn.zengfansheng.rpcalc.R;
public class ResultActivity extends Activity {
    private TextView tv_name;
    private TextView tv_total;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.rp_item);
        
        tv_name = (TextView) this.findViewById(R.id.tv_name);
        tv_total = (TextView) this.findViewById(R.id.tv_total);
        // 获取将当前Activity激活的Intent
        Intent intent = this.getIntent();
        String name = intent.getStringExtra("name");
        int total = intent.getIntExtra("total", 50);
        tv_name.setText("姓名:" + name);
        if (total < 50) {
            tv_total.setTextColor(Color.RED);
            tv_total.setText("人品积分:" + total + "人品过差。。。");
        } else {
            tv_total.setTextColor(Color.BLACK);
            tv_total.setText("人品积分:" + total + "人品还不错。。。");
        }
    }
}

结果:



问题:Caused by: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

Context中有一个startActivity方法,Activity继承自Context,重载了startActivity方法。如果使用 Activity的startActivity方法,不会有任何限制,而如果使用Context的startActivity方法的话,就需要开启一个新的task,遇到上面那个异常的,都是因为使用了Context的startActivity方法。解决办法是,加一个flag。

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);