顯示具有 intent 標籤的文章。 顯示所有文章
顯示具有 intent 標籤的文章。 顯示所有文章

2015年10月13日 星期二

Android app intent other app 從一個App啟動另一個App特定頁面,附帶傳值

1.在App1加入程式碼
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(new ComponentName("com.xxxx.xxxx","com.xxxx.xxxx.SpecialMainActivity")); //包裹名稱,要開啟的頁面
intent.putExtra("value", "test"); //要傳送的值
startActivity(intent);
2.在App2的AndroidManifest.xml加入

        
            
            
                
            
            
        
3.在App2加入接收程式碼
Intent intent = getIntent();
if (intent.hasExtra("value")) {
 String value= intent.getStringExtra("value");
 Log.i("value", value+"");
}else{
 Log.i("value", "null");
}
參考連結:
http://stackoverflow.com/questions/3872063/launch-an-application-from-another-application-on-android

2014年7月18日 星期五

Android share 分享功能

分享至facebook,line之類的
1.activity_main.xml
建立一個分享按鈕

2.MainActivity.java
 private Button shareButton;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  shareButton = (Button) findViewById(R.id.shareButton);
  shareButton.setOnClickListener(new Button.OnClickListener() {
   public void onClick(View v) {
    shareDialog();
   }
  });
 }

 // 分享app
 void shareDialog() {
  
  String shareText = "與你分享的快樂勝過獨自擁有"; //
  //Uri imageUri = Uri.parse("android.resource://" + getPackageName() + "/drawable/" + "ic_launcher"); //分享圖片至gmail、twitter可,line、facebook不行
  //Log.i("imageUri:", imageUri + "");
   Intent shareIntent = new Intent();
   shareIntent.setAction(Intent.ACTION_SEND);
   shareIntent.setType("text/plain"); //文字檔類型
   shareIntent.putExtra(Intent.EXTRA_TEXT, shareText); //傳送文字
   shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
   //shareIntent.setType("image/png"); //圖片png檔類型
  // shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri); //傳送圖片
   startActivity(Intent.createChooser(shareIntent, "分享"));
 }
參考來源:
http://hscc.cs.nctu.edu.tw/~lincyu/Android/ShareText.pdf
http://stackoverflow.com/questions/20333186/how-to-share-imagetext-using-action-send-in-android