編輯:關於 Android 程式設計
借助系統的 ACTION_SEND,我們可以調起系統分享面板,把文本或圖片分享到手機上安裝的各種社交應用。下面是一個完整的示例。
MainActivity 代碼:
package com.hpsvse.weiboshare;
import java.io.File;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button btn1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
btn1 = (Button) findViewById(R.id.button1);
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
shareMsg(MainActivity.this, null, null, null, null);
}
});
}
public static void shareMsg(Context context, String activityTitle, String msgTitle,
String msgText, String imgPath) {
Intent intent = new Intent(Intent.ACTION_SEND);
if (imgPath == null || imgPath.equals("")) {
intent.setType("text/plain"); // 純文本
} else {
File f = new File(imgPath);
if (f != null && f.exists() && f.isFile()) {
intent.setType("image/png");
Uri u = Uri.fromFile(f);
intent.putExtra(Intent.EXTRA_STREAM, u);
}
}
intent.putExtra(Intent.EXTRA_SUBJECT, msgTitle);
intent.putExtra(Intent.EXTRA_TEXT, msgText);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(Intent.createChooser(intent, activityTitle));
}
}
activity_main.xml 代碼:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="分享" />
</LinearLayout>
其餘文件保持不變即可。值得一提的是,最終能分享到的內容多少,取決於你的手機上安裝了多少社交軟件——分享面板會自動列出所有能處理該類型 intent 的應用。
Calabash-Android 上手實作:從截圖到自訂步驟
前一篇說明了如何在 Windows 環境中架好 calabash-android 的測試環境,這篇用一個實例帶大家跑一遍它的基本用法。 範例專案取自:https://gi
APK 反編譯入門:圖片、XML 與代碼資源的提取流程
瞭解 APK 的內部結構,對排查問題、學習他人實現思路、驗證自己應用的加固效果都很有幫助。本文梳理一下提取 APK 中三類資源的基本流程:圖片資源、XML 資源和代碼資源
百度地圖 Android SDK:標注(Marker)的基本用法
標注(Marker)是開發者最常使用的地圖覆蓋物之一,本文就來介紹一些 Marker 最基本的使用方法。 實作目標 1、建構基礎地圖頁面; 2、在地圖中心點處添加 Mar
Android Service 基本概念與常見問題整理
Service(服務)是一個應用程式元件,用於在背景執行長時間的操作,本身不提供使用者介面。其他元件可以啟動一個 Service,即使使用者切換到別的 App,它仍會繼續