●實作練習:預期畫面

=>透過隱性intent將資料傳遞到另一個activity

Screenshot_20190515-145056.jpg    Screenshot_2019-05-15-14-33-45.png

 

●補充說明

(一)隱性Intent(把action寫在 mainifest中)不是指定誰來用,而是只要有在系統註冊這個動作的APP都可以用
 
(二)隱性Intent可以跨app呼叫,因為manifest的資訊在安裝app時,都會註冊在android系統中,所以只要知道設定的action name就可以呼叫不同app的隱性Intent
=>當有兩個action同名字時,就會跳兩個出來讓你選擇(請參考:)
 
EX:同一個APP有兩個activity都註冊了sendMsg這個action,就會出現如下畫面

Screenshot_20190515-150911.jpg

 

 

●程式--AndroidManifest.html

<activity android:name=".Main2Activity">
    <!--隱性intent要先加intent-filter-->
    <intent-filter>
        <!--有在mainfest中註冊這個action的actitvity都可以使用-->
        <!--如果只是自己的程式要用,就可以自定義名稱,如下-->
        <action android:name="sendMsg"/>        
        <!--DEAFAULT就是代表聽到sendMsg時就會跳出來-->
        <category android:name="android.intent.category.DEFAULT"/>   
    </intent-filter>
</activity>

 

●程式--java code

(一)MainActivity.java
public void impicit(View view) {
    Intent in=new Intent();
    //getIntent()取得putExtra的資料
    in.setAction("sendMsg");    
     //get res/values/strings 
    in.putExtra("msg",getResources().getString(R.string.share_content));
    startActivity(in);
}

(二)Main2Activity.java
//getIntent()取得putExtra的資料
Intent in=getIntent();
String str=in.getStringExtra("msg");
TextView text=findViewById(R.id.textResult);
text.setText(str);

 

 

●程式參考(GitHub):Implicit Intent / Share Content 

 

arrow
arrow

    muchone 發表在 痞客邦 留言(0) 人氣()