【问题标题】:OnPrimaryClipChangedListener not workingOnPrimaryClipChangedListener 不工作
【发布时间】:2013-07-07 09:21:13
【问题描述】:

这是我实现 OnPrimaryCLipChangedListener 的代码:

public class PrimaryClipChangedListener implements OnPrimaryClipChangedListener {

@Override
public void onPrimaryClipChanged() {
    // TODO Auto-generated method stub

        // TODO Auto-generated method stub
            Log.d("RAJATH", "copyclip reached");                    
        }

}

注册监听器的我的服务:

package com.example.tryservice;

import android.annotation.SuppressLint;
import android.app.Service;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Intent;
import android.content.ClipboardManager.OnPrimaryClipChangedListener;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

@SuppressLint("NewApi")
public class MyService extends Service{
public MyService() {
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub
    Log.d("RAJATH", "Service Reached");
    ClipboardManager cb = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);       
    cb.addPrimaryClipChangedListener(new PrimaryClipChangedListener());
    return 0;
}
@Override
public IBinder onBind(Intent intent) {
    // TODO: Return the communication channel to the service.
    throw new UnsupportedOperationException("Not yet implemented");
}
}

我有一个启动此服务的活动。这段代码的目的是在后台监听剪贴板的变化。错误在哪里?

【问题讨论】:

  • 您对此有解决方案吗?我在我的 4.3 设备上遇到了同样的问题。没有崩溃,但也没有任何日志。就像它根本没有注册一样。但同样的代码似乎可以在其他设备上运行。

标签: java android clipboard copy-paste clipboardmanager


【解决方案1】:

究竟是什么不起作用? Android 4.3 中存在一个错误,如果您侦听 OnPrimaryClipChangedListener 回调,系统会崩溃。

【讨论】:

  • 我没有收到回电。我没有打印日志消息。
  • 您的系统是否中断...您运行的是哪个版本的 Android?
  • 系统不会崩溃。该服务显示在设置->应用程序->运行
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-10
  • 1970-01-01
  • 2019-01-19
  • 1970-01-01
  • 2018-12-22
相关资源
最近更新 更多