Intent 跳轉 Activity 用法


 1 一、不同應用中的 Activity 傳遞
 2         Intent tIntent=new Intent();
 3         ComponentName tComponentName=new ComponentName("com.oradt.ecard.globalsearch", "com.oradt.ecard.globalsearch.GlobalActivity");
 4         tIntent.setComponent(tComponentName);
 5         tIntent.setAction("android.intent.action.MAIN");
 6         tIntent.putExtra("id", mTimeRing.getId());
 7         mContext.startActivity(tIntent);
 8  1.注解:
 9 
10 1.com.oradt.ecard.globalsearch 是要跳轉應用的包名,即 AndroidManifest.xml 中的 package
11 
12     <manifest xmlns:android="http://schemas.android.com/apk/res/android"
13             package="com.oradt.ecard.globalsearch"
14 
15 2.com.oradt.ecard.globalsearch.GlobalActivity 是要跳轉的 Activity 名+全包名
16         <activity
17             android:name=".GlobalActivity"
18             android:label="@string/app_name" >
19             <intent-filter>
20                 <action android:name="android.intent.action.MAIN" />
21 
22                 <category android:name="android.intent.category.LAUNCHER" />
23             </intent-filter>
24           </activity>
25 就是 android:name 中注冊的名字
26 
27  2.在第 2 個 Activity 中接收數據與平時的一樣
28         Intent tIntent = getIntent();
29         int id = tIntent.getIntExtra("id", -1);
30 
31         if (id > -1) {
32 
33             TextView tId = (TextView) this.findViewById(R.id.tvId);
34             tId.setText(""+id);
35         }
36 
37  3.如果第 2 個 Activity 不是主 Activity,需要設置<intent-filter>
38         <activity android:name="com.oradt.ecard.globalsearch.ContnActivity" >
39             <intent-filter>
40                 <action android:name="com.oradt.action.PLAYER" />
41 
42                 <category android:name="android.intent.category.DEFAULT" />
43             </intent-filter>
44         </activity>
45 
46 其中 android.intent.category.DEFAULT 不是必須的(親自實驗),但 com.oradt.action.PLAYER 是必須的。並且他與 tIntent.setAction("android.intent.action.MAIN");中的內容是對應的。
47         Intent tIntent = new Intent();
48         ComponentName tComponentName=new ComponentName("com.oradt.ecard.globalsearch", "com.oradt.ecard.globalsearch.ContnActivity");
49         tIntent.setComponent(tComponentName);
50         tIntent.setAction("com.oradt.action.PLAYER");
51         mContext.startActivity(tIntent);
52 
53 經實驗,tIntent.setAction("com.oradt.action.PLAYER"); 可不加,但<action android:name="com.oradt.action.PLAYER" /> 必須要有。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM