如何在andoid中从剪贴板中立即获取数据

时间:2017-02-22 11:17:45

标签: android service background copy clipboard

screenshot当用户通过任何应用程序复制一些文本时,我想立即从剪贴板中获取文本,现在我能够看到我早期复制的文本(安装前复制的文本),仅在重新安装应用程序后数据现在正在更新,如何让它立即更新文本?

这是我的主要活动

package com.hackerinside.jaisonjoseph.clipme;

import android.app.Service;
 import android.content.ClipData;
 import android.content.ClipboardManager;
 import android.content.Intent;
 import android.os.Bundle;
 import android.os.IBinder;
 import android.support.annotation.Nullable;
 import android.support.design.widget.FloatingActionButton;
 import android.support.design.widget.Snackbar;
  import android.support.v7.app.AppCompatActivity;
 import android.support.v7.widget.Toolbar;
 import android.view.View;
 import android.view.Menu;
 import android.view.MenuItem;
  import android.widget.TextView;

 public class MainActivity extends AppCompatActivity {








@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);









    ClipboardManager manager=(ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
    ClipData clipData =manager.getPrimaryClip();
    ClipData.Item item=clipData.getItemAt(0);
    String string=item.getText().toString();
    final TextView textView=(TextView)findViewById(R.id.textView);
    textView.setText(string);





    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}

@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);
    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();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

这是我的内容主要

<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"

app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.hackerinside.jaisonjoseph.clipme.MainActivity"
tools:showIn="@layout/activity_main">

<TextView
    android:text="TextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:id="@+id/textView"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />
   </RelativeLayout>

1 个答案:

答案 0 :(得分:0)

尝试这种方式:

final ClipboardManager manager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); 
manager.addPrimaryClipChangedListener(new ClipboardManager.OnPrimaryClipChangedListener() {
  @Override
  public void onPrimaryClipChanged() {
    Log.i("AppLog", "changed to:" + manager.getText());
    // or this for the textview:
    // textView.setText(manager.getText());
  }
});

或者在Kotlin:

val manager = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
manager.addPrimaryClipChangedListener { Log.d("AppLog", manager.text?.toString()) }

文档:https://developer.android.com/reference/android/content/ClipboardManager.html