Created
November 11, 2016 06:13
-
-
Save arunkarnann/1894394eb7ddea22fc72eb44b8815453 to your computer and use it in GitHub Desktop.
clipboard monitoring service code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Service; | |
import android.content.ClipData; | |
import android.content.ClipDescription; | |
import android.content.ClipboardManager; | |
import android.content.ClipboardManager.OnPrimaryClipChangedListener; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.os.IBinder; | |
import com.renault.wordmonitoring.util.ChatHeadRequestManager; | |
import com.renault.wordmonitoring.WordApplication; | |
public class ClipBoardMonitoringService extends Service { | |
IBinder mBinder; | |
Context cm; | |
ClipboardManager cma; | |
@Override | |
public IBinder onBind(Intent arg0) { | |
// TODO Auto-generated method stub | |
return mBinder; | |
} | |
@Override | |
public void onCreate() { | |
// TODO Auto-generated method stub | |
super.onCreate(); | |
cm = WordApplication.applicationContext; | |
cma = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); | |
cma.addPrimaryClipChangedListener(listener); | |
} | |
public int onStartCommand(Intent intent, int flags, int startId) { | |
return START_STICKY; | |
} | |
OnPrimaryClipChangedListener listener = new OnPrimaryClipChangedListener() { | |
@Override | |
public void onPrimaryClipChanged() { | |
// TODO Auto-generated method stub | |
if(WordApplication.getServiceMonitorState()) | |
clipBoardChanged(); | |
} | |
}; | |
private void clipBoardChanged() { | |
ClipData clipData = cma.getPrimaryClip(); | |
if (clipData.getDescription().hasMimeType( | |
ClipDescription.MIMETYPE_TEXT_PLAIN)) { | |
String copiedWord = clipData.getItemAt(0).getText().toString(); | |
if(copiedWord.length() < 20) | |
ChatHeadRequestManager.getInstance().showChatHead(copiedWord); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment