轉自

http://blog.csdn.net/qinyuanpei/article/details/17229489

今天想分享给大家的是在Android中实现分享功能,如果没有了分享,那么再好的应用终究会成为信息孤岛,所以在Andoid应用中实现分享功能就显得十分重要。在这里,分享功能的实现是依靠一个系统的意图来完成的。下面的基本代码:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1.  * Android分享功能的实现  
  2.  * Author:秦元培  
  3.  * 时间:2013129日  
  4.  */  
  5.   
  6. package com.Android.Share;  
  7.   
  8. import android.os.Bundle;  
  9. import android.app.Activity;  
  10. import android.content.Intent;  
  11. import android.view.Menu;  
  12. import android.view.View;  
  13. import android.view.View.OnClickListener;  
  14. import android.widget.Button;  
  15.   
  16. public class MainActivity extends Activity {  
  17.   
  18.     Button BtnShare;  
  19.     @Override  
  20.     protected void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.activity_main);  
  23.         BtnShare=(Button)findViewById(R.id.BtnShare);  
  24.         BtnShare.setOnClickListener(new OnClickListener()  
  25.         {  
  26.             @Override  
  27.             public void onClick(View v)   
  28.             {  
  29.                 Intent intent=new Intent(Intent.ACTION_SEND);  
  30.                 intent.setType("text/plain");  
  31.                 intent.putExtra(Intent.EXTRA_SUBJECT, "分享");  
  32.                 intent.putExtra(Intent.EXTRA_TEXT, "我是Robin,我正在使用Android分享功能为大家分享这条信息,欢迎大家访问我的博客http://blog.csdn.net/qinyuanpei");  
  33.                 startActivity(Intent.createChooser(intent, "分享到"));  
  34.             }  
  35.               
  36.         });  
  37.     }  
  38.   
  39.     @Override  
  40.     public boolean onCreateOptionsMenu(Menu menu) {  
  41.         // Inflate the menu; this adds items to the action bar if it is present.  
  42.         getMenuInflater().inflate(R.menu.main, menu);  
  43.         return true;  
  44.     }  
  45.   
  46. }  

          效果如下:

arrow
arrow
    全站熱搜

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