Reputation: 2311
I am trying to do a Notification Action (for Android 4.1+) that will copy some text to the clipboard. I read the reference about the Copy-Paste feature and now I have this:
ClipboardManager clipboard = (ClipboardManager)
mContext.getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("simple text","Hello, World!");
I don't know how to put it into an Intent that will be put into a PendingIntent.
If you can give me some example and explain it - it will be great! I'm a new with android developing. Thank you in advance.
Edit: I found this: https://stackoverflow.com/a/12601766/1866009 but I don't understand it correctly.
Upvotes: 3
Views: 4578
Reputation: 2311
With android developer's help the problem was solved by this code:
BroadcastReceiver brCopy = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
ClipboardManager clipboard = (ClipboardManager)
mContext.getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("label", "text");
clipboard.setPrimaryClip(clip);
Toast.makeText(mContext, "Copied!", Toast.LENGTH_SHORT).show();
}
};
IntentFilter intentFilter = new IntentFilter("com.example.ACTION_COPY");
mContext.registerReceiver(brCopy, intentFilter);
Intent copy = new Intent("com.example.ACTION_COPY");
PendingIntent piCopy = PendingIntent.getBroadcast(mContext, 0, copy, PendingIntent.FLAG_CANCEL_CURRENT);
mBuilder.addAction(android.R.drawable.ic_menu_copy, "Copy", piCopy);
Upvotes: 8
Reputation: 115952
check this tutorial about android notifications actions :
http://www.vogella.com/articles/AndroidNotifications/article.html
when creating the pendingIntent , give it a custom intent that will be received by your broadcastReceiver (defined in your manifest, example here ) , and then do whatever you wish with it (for example copy to the clipboard) .
Upvotes: 4