2014年9月15日 星期一

Android 分享文字訊息至Line

Android 指定開Line App傳入文字訊息

通常Android要開啟第三方App需知道對方app的package name


 PackageManager pm = mContext.getPackageManager();
        List<applicationinfo>  appList =  pm.getInstalledApplications(0);
        for(ApplicationInfo app: appList )
        {
            Log.i("info","app:" +  app );
            if( app.packageName.equals(LINE_PACKAGE_NAME))
            {

                return true;
            }
        }

上面的方法 可以抓出手機內所有的App package name

而你會發現Line的App Package Name的名稱是 jp.naver.line.android

故當程式可用此方式來檢查是否裝此app

若要從程式碼傳送文字至 Line訊息的話


intent.setAction(Intent.ACTION_SEND);
intent = mContext.getPackageManager().getLaunchIntentForPackage(AppConfig.LINE_PACKAGE_NAME);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "this is test! oh ya!");
mContext.startActivity(intent);

上面是送文字訊息

而AndroidManifest.xml 記得要加接收到ACTION_SEND的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 >


沒有留言: