安卓--组建通信

Posted feifei

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了安卓--组建通信相关的知识,希望对你有一定的参考价值。

实验目的:

熟悉和掌握android组件间通信的方式和技巧。

实验要求:

1. 运行课本的示例程序,理解组件通信的方式和过程

2.设计一个主Activity和一个子Activity(Sub-Activity),使用主Activity上的按钮启动子Activity,并将子Activity的一些信息返回给主Activity,并显示在主Activity上。

 

 1 package com.flyuz.myapplication;
 2 
 3 import android.content.Intent;
 4 import android.net.Uri;
 5 import android.support.v7.app.AppCompatActivity;
 6 import android.os.Bundle;
 7 import android.view.View;
 8 import android.widget.Button;
 9 import android.widget.TextView;
10 
11 public class MainActivity extends AppCompatActivity {
12     Button bt1;
13     Button bt2;
14     TextView tv;
15     final int SUBACTIVITY1 = 1;
16     final int SUBACTIVITY2 = 2;
17     @Override
18     protected void onCreate(Bundle savedInstanceState) {
19         super.onCreate(savedInstanceState);
20         setContentView(R.layout.activity_main);
21         setTitle("MainActivity");
22         bt1 = (Button) findViewById(R.id.bt1);
23         bt2 = (Button) findViewById(R.id.bt2);
24         tv = (TextView) findViewById(R.id.tv);
25         bt1.setOnClickListener(new View.OnClickListener() {
26             @Override
27             public void onClick(View view) {
28                 Intent intent = new Intent(MainActivity.this, NewActivity1.class);
29                 startActivityForResult(intent, SUBACTIVITY1);
30             }
31         });
32         bt2.setOnClickListener(new View.OnClickListener() {
33             @Override
34             public void onClick(View view) {
35                 Intent intent = new Intent(MainActivity.this, NewActivity2.class);
36                 startActivityForResult(intent, SUBACTIVITY2);
37             }
38         });
39     }
40 
41     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
42         super.onActivityResult(requestCode, resultCode, data);
43         switch (requestCode) {
44             case SUBACTIVITY1:
45                 if (resultCode == 1) {
46                     Uri uriData = data.getData();
47                     tv.setText(uriData.toString());
48                 }
49             case SUBACTIVITY2:
50                 if (resultCode == -1) {
51                     Uri uriData = data.getData();
52                     tv.setText(uriData.toString());
53                 }
54         }
55     }
56 }
MainActivity
 1 package com.flyuz.myapplication;
 2 
 3 import android.net.Uri;
 4 import android.support.v7.app.AppCompatActivity;
 5 import android.os.Bundle;
 6 import android.view.View;
 7 import android.widget.Button;
 8 import android.widget.EditText;
 9 import android.content.Intent;
10 
11 public class NewActivity1 extends AppCompatActivity {
12     Button btOk;
13     EditText et;
14     @Override
15     protected void onCreate(Bundle savedInstanceState) {
16         super.onCreate(savedInstanceState);
17         setContentView(R.layout.activity_new1);
18         setTitle("NewActivity1");
19         btOk = (Button)findViewById(R.id.btOK);
20         et = (EditText)findViewById(R.id.et);
21         btOk.setOnClickListener(new View.OnClickListener() {
22             @Override
23             public void onClick(View view) {
24                 String str = et.getText().toString();
25                 Uri data = Uri.parse("来自NewActivity1的消息" + str);
26                 Intent result = new Intent(null, data);
27                 setResult(1, result);
28                 finish();
29             }
30         });
31     }
32 }
NewActivity1
 1 package com.flyuz.myapplication;
 2 
 3 import android.net.Uri;
 4 import android.support.v7.app.AppCompatActivity;
 5 import android.os.Bundle;
 6 import android.view.View;
 7 import android.widget.Button;
 8 import android.widget.EditText;
 9 import android.content.Intent;
10 
11 public class NewActivity2 extends AppCompatActivity {
12     Button btOk;
13     EditText et;
14     @Override
15     protected void onCreate(Bundle savedInstanceState) {
16         super.onCreate(savedInstanceState);
17         setContentView(R.layout.activity_new1);
18         setTitle("NewActivity2");
19         btOk = (Button)findViewById(R.id.btOK);
20         et = (EditText)findViewById(R.id.et);
21         btOk.setOnClickListener(new View.OnClickListener() {
22             @Override
23             public void onClick(View view) {
24                 String str = et.getText().toString();
25                 Uri data = Uri.parse("来自NewActivity2的消息" + str);
26                 Intent result = new Intent(null, data);
27                 setResult(-1, result);
28                 finish();
29             }
30         });
31     }
32 }
NewActivity2
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:app="http://schemas.android.com/apk/res-auto"
 4     xmlns:tools="http://schemas.android.com/tools"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7     android:gravity="center"
 8     tools:context=".MainActivity"
 9     android:orientation="vertical">
10     <Button
11         android:id="@+id/bt1"
12         android:layout_width="match_parent"
13         android:layout_height="wrap_content"
14         android:layout_weight="0.2"
15         android:text="进入NewActivity1!" />
16 
17     <Button
18         android:id="@+id/bt2"
19         android:layout_width="match_parent"
20         android:layout_height="wrap_content"
21         android:layout_weight="0.2"
22         android:text="进入NewActivity2!" />
23     <TextView
24         android:id="@+id/tv"
25         android:layout_width="match_parent"
26         android:layout_height="wrap_content"
27         android:layout_weight="0.6"
28         android:text="" />
29 
30 </LinearLayout>
layout\\activity_main.xml
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="vertical"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:gravity="center" >
 7 
 8     <LinearLayout
 9         android:layout_width="match_parent"
10         android:layout_height="wrap_content"
11         android:orientation="horizontal"
12         android:gravity="center" >
13 
14         <TextView
15             android:id="@+id/tv"
16             android:layout_width="wrap_content"
17             android:layout_height="wrap_content"
18             android:layout_weight="0.2"
19             android:text="回信:" />
20 
21         <EditText
22             android:id="@+id/et"
23             android:layout_width="wrap_content"
24             android:layout_height="wrap_content"
25             android:layout_weight="0.8" />
26     </LinearLayout>
27 
28 
29     <Button
30         android:id="@+id/btOK"
31         android:layout_width="match_parent"
32         android:layout_height="wrap_content"
33         android:text="确定" />
34 </LinearLayout>
layout\\activity_new1.xml
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     package="com.flyuz.myapplication">
 4 
 5     <application
 6         android:allowBackup="true"
 7         android:icon="@mipmap/ic_launcher"
 8         android:label="@string/app_name"
 9         android:roundIcon="@mipmap/ic_launcher_round"
10         android:supportsRtl="true"
11         android:theme="@style/AppTheme">
12         <activity android:name=".MainActivity">
13             <intent-filter>
14                 <action android:name="android.intent.action.MAIN" />
15 
16                 <category android:name="android.intent.category.LAUNCHER" />
17             </intent-filter>
18         </activity>
19 
20         <activity android:name=".NewActivity1">
21             <intent-filter>
22                 <action android:name="android.intent.action.VIEW" />
23 
24                 <category android:name="android.intent.category.DEFAULT" />
25             </intent-filter>
26         </activity>
27 
28         <activity android:name=".NewActivity2">
29             <intent-filter>
30                 <action android:name="android.intent.action.VIEW" />
31 
32                 <category android:name="android.intent.category.DEFAULT" />
33             </intent-filter>
34         </activity>
35 
36     </application>
37 
38 </manifest>
AndroidManifest.xml

 隐式启动:

1         bt.setOnClickListener(new View.OnClickListener() {
2             @Override
3             public void onClick(View v) {
4                 Intent intent = new Intent("android.intent.action.VIEW"); //<action android:name="android.intent.action.VIEW" /> 这里的
5                 Toast.makeText(getApplicationContext(), "隐式启动", Toast.LENGTH_SHORT).show();
6                 startActivityForResult(intent, SUBACTIVITY);
7             }
8         });
MainActivity

 

以上是关于安卓--组建通信的主要内容,如果未能解决你的问题,请参考以下文章

java 代码片段【安卓】

片段从一开始就没有显示 |安卓工作室

安卓。片段 getActivity() 有时返回 null

在tablayout片段之间进行通信[重复]

与另一个片段通信的片段接口

React native中的组建通知通信: