Activityデータ転送--Intent表示、暗黙的、異なるアプリケーション間で起動
4914 ワード
******************************************************************************************************************
Mainレイアウトbutton------secondレイアウトにはTextViewとImageViewがあります
MainActivity
SecondActivity
+++++++++++++++++++++++Intent+++++++++++++++++++++
起動の表示
暗黙の起動
String ACTION = "android.intent.action.three";
異なるアプリケーション間でActivityインタフェースを起動
Mainレイアウトbutton------secondレイアウトにはTextViewとImageViewがあります
MainActivity
package com.example.activitydemo;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
//
// Person person = new Person(11, "wyf", "bj");
Bundle bundle = new Bundle();
// bundle.putSerializable("person", person);
// bundle.putString("aaa", "bbb");
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
bundle.putParcelable("bitmap", bitmap);
intent.putExtras(bundle);
startActivity(intent);
}
});
}
}
SecondActivity
package com.example.activitydemo;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
public class SecondActivity extends Activity {
private TextView tv;
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
tv = (TextView) findViewById(R.id.tv);
imageView = (ImageView) findViewById(R.id.imageView);
Intent intent = getIntent();
if (intent !=null) {
// Person p = (Person) intent.getSerializableExtra("person");
// String stringExtra = intent.getStringExtra("aaa");
Bitmap bitmap = intent.getParcelableExtra("bitmap");
imageView.setImageBitmap(bitmap);
// tv.setText(p.toString()+stringExtra);
}
}
}
package com.example.activitydemo;
import java.io.Serializable;
public class Person implements Serializable {
private static final long serialVersionUID = 1L;
private int age;
private String name;
private String address;
public Person(int age, String name, String address) {
super();
this.age = age;
this.name = name;
this.address = address;
}
@Override
public String toString() {
return "Person [age=" + age + ", name=" + name + ", address=" + address
+ "]";
}
}
+++++++++++++++++++++++Intent+++++++++++++++++++++
起動の表示
<span style="font-size:14px;"> button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
// intent.setClass(MainActivity.this, SecondAty.class);
// intent.setClassName(MainActivity.this, "com.example.intentdemo.SecondAty");
intent.setClassName("com.example.intentdemo", "com.example.intentdemo.SecondAty");
startActivity(intent);
}
});</span>
暗黙の起動
String ACTION = "android.intent.action.three";
<span style="font-size:14px;">
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(ACTION);
startActivity(intent);
}
});</span>
<span style="font-size:14px;"> <activity
android:name=".ThridAty" >
<intent-filter>
<action android:name="android.intent.action.three" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity></span>
異なるアプリケーション間でActivityインタフェースを起動
Intent intent = new Intent();
ComponentName cn = new ComponentName("com.vzhen.secureservice", "com.vzhen.secureservice.ui.LandScapeActivity");
intent.setComponent(cn);
startActivity(intent);
Intent intent = new Intent();
intent.setClassName("com.vzhen.secureservice", "com.vzhen.secureservice.ui.LandScapeActivity");
startActivity(intent);