2015年8月29日 星期六

【Android】簡單實作分享ShareActionProvider

這次利用ShareActionProvider來完成一個簡單的分享,雖說是一個簡單的實作,

但也讓我學了好幾天,先分享一點心路歷程給大家,本來要使用
android.widget.ShareActionProvider,

後來改用android.support.v7.widget.ShareActionProvider的版本,因為我extends ActionBarActivity,使得只能使用support.v7的版本。


  • 首先於meun_main.xml,新增一個item並於item中加入下面這行app:actionProviderClass="android.support.v7.widget.ShareActionProvider"




  • 幾段簡單的code如下,先將 mShareActionProvider建立起來並取得R.id.menu_item_share

private ShareActionProvider mShareActionProvider;
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);

        // Set up ShareActionProvider's default share intent
        MenuItem shareItem = menu.findItem(R.id.menu_item_share);
        mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(shareItem);

        setShareIntent(setIntent());

        return true;
    }



  • setIntent()則是要送出的分享Intent,我們用最簡單傳遞文字來測試

private Intent setIntent(){

        Intent sendIntent = new Intent();
        sendIntent.setAction(Intent.ACTION_SEND);
        sendIntent.setType("text/plain");
        sendIntent.putExtra(Intent.EXTRA_TEXT, "分享的文字");

        
        return sendIntent;
      
    }


  • setShareIntent()這段可以直接搬去上面的onCreateOptionsMenu,來確認mShareActionProvider是否為null並設置ShareIntent

private void setShareIntent(Intent shareIntent) {
      if (mShareActionProvider != null) {
           mShareActionProvider.setShareIntent(shareIntent);
        }
    }


  • 如果想更改icon則要利用以下這段code,actionModeShareDrawable才能更改share icon




結果如下:


























※你會發現ShareActionProvider會將你選取過的app,放置在你的icon右邊,如果想避開目前找到的解答都不是相當完整,後來我用createChooser改變顯示的方式,也希望有找到解答的高手大大們能分享一下。

※在給大家一個關於上面問題的關鍵方法:

  • mShareActionProvider.setShareHistoryFileName("custom_share_history.xml");
只要設為null,其實就會取消上面那個效果,但分享的功能就會產生問題。

google一下會找到幾個方法去解決,一個為下面的 OnShareTargetSelectedListener

http://whutec.sinaapp.com/2012/12/how-do-you-turn-off-share-history-when-using-shareactionprovider/

這也是一個解法,但我資質駑鈍,實在不是很了解 = =!!

http://stackoverflow.com/questions/13395601/android-shareactionprovider-with-no-history

如果哪天找到解法,會再跟大家分享的!!

參考連結:
官網其他範例
http://developer.android.com/intl/zh-tw/training/building-content-sharing.html

ActionBarCompat with ShareActionProvider
http://android-er.blogspot.tw/2013/12/actionbarcompat-with-share.html

如何取得 Android app 的 Package Name 並透過 Intent 發送訊息
http://cloudchen.logdown.com/posts/159965/how-to-find-android-app-package-name-use-to-sent-message-through-intent

How to change icon
http://stackoverflow.com/questions/23846127/android-custom-icon-shareactionprovider

2015年8月7日 星期五

【Android】無法更改action bar backgroud問題


想要更改action bar backgroud的顏色,在google上能夠找到簡單的範例

在style裡利用下面這段code,通常就能夠改變action bar的顏色,但你編譯後會發現並沒有改變,問題出在哪?




答案是:AppCompat
因為使用了AppCompat這個library,所以有版本上的問題

兩個版本的寫法分別是

  • item name = "android:actionBarStyle"
  • item name = "actionBarStyle"
所以我們只要在style中加入,兩種版本的寫法就能夠成功運行

























參考連結:

http://developer.android.com/guide/topics/ui/actionbar.html (官方 請找Example theme有詳細說明)


http://stackoverflow.com/questions/27556031/cant-change-background-colour-of-actionbar