Android Open Source - android-clipboard Main Activity






From Project

Back to project page android-clipboard.

License

The source code is released under:

MIT License

If you think the Android project android-clipboard listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.example.myclipboard;
/*  w  w  w.  j a  va2s  .  c o  m*/
import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager; // ? androix.text.? ???????????? android.context.? ? import
import android.content.Intent;
import android.content.ClipData.Item;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
  EditText mEdit;
  TextView mText;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    mEdit = (EditText)findViewById(R.id.editText1);
    mText = (TextView)findViewById(R.id.textView1);
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
      return true;
    }
    return super.onOptionsItemSelected(item);
  }
  
  // ???????
  public void buttonMethodCopy(View button){
    try{
      // ClipboardManager????
      ClipboardManager cm = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
      
      // ???????
      String s = mEdit.getText().toString();
      
      // ?????????????
      Intent intent = new Intent(this, SubActivity.class);
      
      // ClipData????????????????????????????
      ClipData data = ClipData.newPlainText("sample data", s);
      data.addItem(new ClipData.Item("ppppp"));
      data.addItem(new ClipData.Item(intent)); // ???????
      cm.setPrimaryClip(data);
    }
    catch(Exception ex){
      mText.setText("Error: " + ex.toString());
    }
  }
  
  // ????????
  public void buttonMethodPaste(View button){
    try{
      // ClipboardManager????
      ClipboardManager cm = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
      if(cm == null)throw new Exception("Clipboard Manager not found");
      
      // ClipData????
      ClipData data = cm.getPrimaryClip();
      if(data == null)throw new Exception("ClipData not found");

      // ???????
      String s = "";
      s += "--------------------\n";
      s += "Summary: " + data.toString() + "\n";
      s += "--------------------\n";
      
      // ?????????
      s += "ItemCount: " + data.getItemCount() + "\n";
      
      // ?????????
      for(int i = 0; i < data.getItemCount(); i++){
        ClipData.Item item = data.getItemAt(i);
        // getText?????
        CharSequence cs = item.getText();
        if(cs != null){
          s += "Item[" + i + "]: " + cs.toString() + "\n";
          continue;
        }
        // getIntent?????
        Intent intent = item.getIntent();
        if(intent != null){
          s += "Item[" + i + "]: " + intent.toString() + "\n";
          continue;
        }
      }
      
      // ??
      mText.setText(s);
    }
    catch(Exception ex){
      mText.setText("Error: " + ex.toString());
    }
  }
  
  // ??????????Intent????????????
  public void buttonMethodPaste2(View button){
    try{
      // ClipboardManager????
      ClipboardManager cm = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
      if(cm == null)throw new Exception("Clipboard Manager not found");
      
      // ClipData????
      ClipData data = cm.getPrimaryClip();
      if(data == null)throw new Exception("ClipData not found");

      // ?????????
      for(int i = 0; i < data.getItemCount(); i++){
        ClipData.Item item = data.getItemAt(i);
        // getIntent?????
        Intent intent = item.getIntent();
        if(intent != null){
          mText.setText("Launch intent of ClipItem[" + i + "]");
          this.startActivity(intent);
          return;
        }
      }
      
      // Intent???????????????????????
      mText.setText("Intent not found in ClipData");
    }
    catch(Exception ex){
      mText.setText("Error: " + ex.toString());
    }
  }
}




Java Source Code List

com.example.myclipboard.MainActivity.java
com.example.myclipboard.SubActivity.java