2016-05-04 14 views
0

ShareActionProviderを使用して共有アイコンを作成しました。 MainActivityクラスでは、私は以下のものを持っている:Androidの共有インテントの内容を更新します

public class MainActivity extends AppCompatActivity{ 
.... 
private ShareActionProvider mShareActionProvider; 
... 
@Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.main, menu); 

     // Locate MenuItem with ShareActionProvider 
     MenuItem item = menu.findItem(R.id.action_share); 
     // Fetch and store ShareActionProvider 
     mShareActionProvider = 
       (ShareActionProvider) MenuItemCompat.getActionProvider(item); 

     // call the intent 
     setShareIntent(); 
     return true; 
    } 

    private void setShareIntent() { 

     Intent myShareIntent = new Intent(); 
     myShareIntent.setAction(Intent.ACTION_SEND); 
     myShareIntent.setType("text/plain"); 

     // get the content of the text view 
     myShareIntent.putExtra(Intent.EXTRA_TEXT, myTextView.getText()); 

     mShareActionProvider.setShareIntent(myShareIntent); 

    } 

問題は、ユーザーが共有アイコンをクリックしたときにmyTextView変更の内容とは、デフォルトのテキストが共有されていることです。

これは、onCreateOptionsMenuのインテントを呼び出しているためです。現在のコンテンツがmyTextViewに保存されるようにインテントを更新するにはどうすればよいですか?

答えて

0

次の方法でオーバーライドする必要があります。

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    //your code 
} 

をそしてそこにあなたの方法setShareIntent()を呼び出します。これはもちろん、オプションのクリックでその動作をトリガーしたい場合、別のボタンなどがある場合は、onClickListenerを追加するだけです。

1

ActivityのフィールドにIntentを保持し、共有する値が変更されたときにフィールドにputExtra()を呼び出します。 this sample app

、私はEditTextを持っているとしてたときに、ユーザーの種類のものIntent余分を更新したい:私は余分を更新するだけでなく、再びsetShareIntent()を呼び出すことを選択

/*** 
    Copyright (c) 2012 CommonsWare, LLC 
    Licensed under the Apache License, Version 2.0 (the "License"); you may not 
    use this file except in compliance with the License. You may obtain a copy 
    of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required 
    by applicable law or agreed to in writing, software distributed under the 
    License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 
    OF ANY KIND, either express or implied. See the License for the specific 
    language governing permissions and limitations under the License. 

    From _The Busy Coder's Guide to Android Development_ 
    https://commonsware.com/Android 
*/ 

package com.commonsware.android.sap; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.text.Editable; 
import android.text.TextWatcher; 
import android.view.Menu; 
import android.widget.EditText; 
import android.widget.ShareActionProvider; 
import android.widget.Toast; 

public class MainActivity extends Activity implements 
    ShareActionProvider.OnShareTargetSelectedListener, TextWatcher { 
    private ShareActionProvider share=null; 
    private Intent shareIntent=new Intent(Intent.ACTION_SEND); 
    private EditText editor=null; 

    @Override 
    public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    setContentView(R.layout.activity_main); 

    shareIntent.setType("text/plain"); 
    editor=(EditText)findViewById(R.id.editor); 
    editor.addTextChangedListener(this); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.actions, menu); 

    share= 
     (ShareActionProvider)menu.findItem(R.id.share) 
           .getActionProvider(); 
    share.setOnShareTargetSelectedListener(this); 

    return(super.onCreateOptionsMenu(menu)); 
    } 

    @Override 
    public boolean onShareTargetSelected(ShareActionProvider source, 
             Intent intent) { 
    Toast.makeText(this, intent.getComponent().toString(), 
        Toast.LENGTH_LONG).show(); 

    return(false); 
    } 

    @Override 
    public void afterTextChanged(Editable s) { 
    shareIntent.putExtra(Intent.EXTRA_TEXT, s.toString()); 
    share.setShareIntent(shareIntent); 
    } 

    @Override 
    public void beforeTextChanged(CharSequence s, int start, int count, 
           int after) { 
    // ignored 
    } 

    @Override 
    public void onTextChanged(CharSequence s, int start, int before, 
          int count) { 
    // ignored 
    } 
} 

。それが必要かどうかは分かりません。

+0

申し訳ありませんが、これは非常に混乱しているので、 'onCreate'にインテントを作成しますか? –

+0

@RainMan:フィールド初期化子に 'Content'や何かに依存しないので、実際に' Intent'を作成します。 'afterTextChanged()'の呼び出しごとにまったく新しい 'Intent'オブジェクトを作成できましたが、それは無駄に思えました。 – CommonsWare

関連する問題