从任何地方复制文本时打开我的应用程序

open my app when text is copied from anywhere

我想 打开我的应用程序并显示复制的文本 每当我从 android 中的任何地方复制文本时。我该怎么做?

  public class MainActivity extends Activity
{

    Button paste;
    TextView display;
    private ClipData clipdata;
    private android.content.ClipboardManager mgr;

    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        paste = (Button) findViewById(R.id.paste);
        display = (TextView) findViewById(R.id.textView1);

        mgr = (android.content.ClipboardManager) getSystemService(CLIPBOARD_SERVICE);

        paste.setOnClickListener(new OnClickListener()
        {
            public void onClick(View arg0)
            {

                try
                {
                    clipdata = mgr.getPrimaryClip();
                    ClipData.Item item = clipdata.getItemAt(0);

                    String text = item.getText().toString();

                    display.setText(display.getText() + "\n" + text);

                    Toast.makeText(getApplicationContext(), "Text Pasted",
                            Toast.LENGTH_SHORT).show();
                } catch (Exception e)
                {
                    Toast.makeText(getApplicationContext(),
                            "Error : " + e.getMessage(), Toast.LENGTH_SHORT)
                            .show();
                }
            }
        });
    }

}

试试这个,

 ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); 
 ClipData clip = ClipData.newPlainText(label, text);
 clipboard.setPrimaryClip(clip);

它在我的应用程序中运行良好。

您可以像 activity

一样尝试在清单文件中注册您的服务
<service name:".DemoService"/>

开始服务你的代码像 startActivity(new Intent(context, YOURACTIVITY.class)) for service startService(new Intent(context, YOURSERVICE.class))

 public class DemoService extends Service {
  ClipboardManager clipboardManager;
               private OnPrimaryClipChangedListener clipListener = new OnPrimaryClipChangedListener() {
                 public void onPrimaryClipChanged() {
                    String charSequence = clipboardManager.getPrimaryClip().getItemAt(0).getText().toString();
                    showToast("Copy:\n" + charSequence);

                 }
                };
    }

您需要使用 addPrimaryClipChangedListener :

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

ClipboardManager
.addPrimaryClipChangedListener(new ClipboardManager.OnPrimaryClipChangedListener() {
  @Override
  protected void onPrimaryClipChanged() {
    Log.i("clipboard", "changed text : " + ClipboardManager.getText());
  }
});

以下是一个工作代码,它会在您将内容复制到其他任何地方时启动您的应用程序,它会在应用程序首次启动时在后台使用 ClipboardMonitorService 服务 运行:

Manifest.xml 中将以下行放在 <application> 标签下:

<service android:name=".ClipboardMonitorService" />

然后在MainActivityclass中启动onCreate()中的服务如下:

startService(new Intent(this, ClipboardMonitorService.class));

ClipboardMonitorService class:

package com.kushal.webtest;

import android.app.Service;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Intent;
import android.os.IBinder;



public class ClipboardMonitorService extends Service {

    private ClipboardManager mClipboardManager;

    @Override
    public void onCreate() {
        super.onCreate();

        mClipboardManager =
                (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
        mClipboardManager.addPrimaryClipChangedListener(
                mOnPrimaryClipChangedListener);

        System.out.println("Service started running..");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        if (mClipboardManager != null) {
            mClipboardManager.removePrimaryClipChangedListener(
                    mOnPrimaryClipChangedListener);
        }
    }

    @Override
    public IBinder onBind(Intent intent) {

        return null;
    }

    private ClipboardManager.OnPrimaryClipChangedListener mOnPrimaryClipChangedListener =
            new ClipboardManager.OnPrimaryClipChangedListener() {
                @Override
                public void onPrimaryClipChanged() {

                    String charSequence = mClipboardManager.getPrimaryClip().getItemAt(0).getText().toString();
                    System.out.println("Copied Link : ====================" + charSequence);

                    Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                    intent.putExtra("copiedLink", charSequence);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }
            };



}