每当我从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();
}
}
});
}
}
答案 0 :(得分:1)
您需要使用addPrimaryClipChangedListener
:
ClipboardManager
.addPrimaryClipChangedListener(new ClipboardManager.OnPrimaryClipChangedListener() {
@Override
protected void onPrimaryClipChanged() {
Log.i("clipboard", "changed text : " + ClipboardManager.getText());
}
});
答案 1 :(得分:0)
试试这个,
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText(label, text);
clipboard.setPrimaryClip(clip);
它在我的应用中运行良好。
答案 2 :(得分:0)
您可以尝试在清单文件中注册您的服务,就像您在活动中注册一样
<service name:".DemoService"/>
用于启动服务的代码,例如startActivity(new Intent(context,YOURACTIVITY.class)),用于服务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);
}
};
}
答案 3 :(得分:0)
以下是一个工作代码,只要您在其他地方复制任何内容,它就会启动您的应用程序,它使用在首次启动该应用程序时在后台运行的 ClipboardMonitorService 服务:
在 Manifest.xml 中,将以下行放在<application>
标签下:
<service android:name=".ClipboardMonitorService" />
然后在 MainActivity 类中,按如下所示在onCreate()
中启动服务:
startService(new Intent(this, ClipboardMonitorService.class));
ClipboardMonitorService 类:
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);
}
};
}