2013年2月18日 星期一

共享功能-分享與接收

這一篇來談談Android手機上把文字資料發送到其他應用程式去執行其他動作的功能,比如說發送到雲端硬碟、LINE、Facebook、mail...等等,之後再來看看如何自製程式來接收分享資訊。



在網路上搜索了一下發現了一篇簡單易懂的文件,於是參考一下之後來實作一下成果。
資料來源:http://hscc.cs.nctu.edu.tw/~lincyu/Android/ShareText.pdf

範例圖


起始畫面
編輯文字
Chooser選單



程式碼

layout就省略了,貼上主要程式部分。
ShareEx.java:

public class ShareEx extends Activity {
    private Button btnStart;
    private EditText etShareText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share_ex);
        btnStart=(Button) findViewById(R.id.btnStart);
        btnStart.setOnClickListener(btnOnClick);
    }

    protected OnClickListener btnOnClick =new OnClickListener(){
        public void onClick(View v){
            openDialog();
        }
    };
    void openDialog(){
        LayoutInflater li =LayoutInflater.from(this);
        View dialogView=li.inflate(R.layout.dialog, null);
        etShareText=(EditText) dialogView.findViewById(R.id.etShareText);
        Button btnShareto=(Button) dialogView.findViewById(R.id.btnShareto);
        btnShareto.setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                String text=etShareText.getEditableText().toString();
                Intent intent=new Intent(Intent.ACTION_SEND);
                intent.setType("text/plain");
                intent.putExtra(Intent.EXTRA_TEXT, text);
                startActivity(Intent.createChooser(intent, "Share"));
            }
        });
        AlertDialog.Builder ad=new AlertDialog.Builder(this);
        ad.setView(dialogView);
        ad.show();
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_share_ex, menu);
        return true;
    }

}

如果自製的APP想在chooser列表上出現,又該怎麼做呢?
再來看看下面範例。

範例圖

ShareDestination出現在選單上
顯示分享的文字

程式碼

1. AndroidManifest.xml中設定Activity能處理ACTION為Intent.ACTION_SEND的Intent,且進一步指定能夠處理的資料為text/plain。

        <activity
            android:name="com.example.sharedestination.ShareDestination"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter >
                <action android:name="android.intent.action.SEND"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <data android:mimeType="text/plain"/>
            </intent-filter>
        </activity>

2. 主程式部分中,宣告Intent接收傳送來的Intent的內容,然後判斷是否為ACTION_SEND,若為"是"則處理分享而來的資訊。
ShareDestination.java:

public class ShareDestination extends Activity {
    private TextView textShare;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share_destination);
        textShare=(TextView) findViewById(R.id.textShare);
        Intent intent=getIntent();
        if(intent.getAction().equals(Intent.ACTION_SEND)){
            textShare.setText(intent.getStringExtra(Intent.EXTRA_TEXT));
        }
    }

}

補充:
要傳圖片的處理參考

沒有留言:

張貼留言