I have a program that monitors clipboard by getting copied text to the clipboard
I want each time user copies some text to the clipboard i print it
first i have used Toast
to print the copied text by the user and it works fine
but when i want to print the copied text in an AlertDialog
it say "Unfortunately your program has stooped"
What i do to print that text in an alert box?
here is the Activity that monitors the clipboard text
package on.boot.completed;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ClipboardManager;
import android.os.Bundle;
@SuppressLint("NewApi")
public class OnTextChanged extends Activity implements ClipboardManager.OnPrimaryClipChangedListener{
ClipboardManager clipBoard;
AlertDialog.Builder alertDialog;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
clipBoard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
clipBoard.addPrimaryClipChangedListener( this );
}
@SuppressWarnings("deprecation")
public void onPrimaryClipChanged() {
/* it works fine!!!!
*
Toast.makeText(this, clipBoard.getText(), Toast.LENGTH_LONG).show();
*/
alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("Title");
alertDialog.setMessage(clipBoard.getText()+"");
alertDialog.setNegativeButton("OK", null);
AlertDialog alert = alertDialog.create();
alert.show();
}
}
And here is my Service that runs at the background
package on.boot.completed;
import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
@SuppressLint("NewApi")
public class StarterService extends Service {
private static final String TAG = "MyService";
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
Toast.makeText(this, "My Service stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
}
/**
* The started service opens the Activity.
*/
@Override
public void onStart(Intent intent, int startid) {
Intent intents = new Intent(getBaseContext(), OnTextChanged.class);
intents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intents);
}
}
And here is my Log cat Message
11-23 15:39:30.180: I/dalvikvm(2281): Turning on JNI app bug workarounds for target SDK version 7...
11-23 15:39:30.256: D/(2281): HostConnection::get() New Host Connection established 0xb925f780, tid 2281
11-23 15:39:37.460: D/dalvikvm(2281): GC_CONCURRENT freed 73K, 3% free 11063K/11335K, paused 0ms+1ms, total 3ms
11-23 15:39:37.468: D/AndroidRuntime(2281): Shutting down VM
11-23 15:39:37.468: W/dalvikvm(2281): threadid=1: thread exiting with uncaught exception (group=0xa6227288)
11-23 15:39:37.472: E/AndroidRuntime(2281): FATAL EXCEPTION: main
11-23 15:39:37.472: E/AndroidRuntime(2281): android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@53658204 is not valid; is your activity running?
11-23 15:39:37.472: E/AndroidRuntime(2281): at android.view.ViewRootImpl.setView(ViewRootImpl.java:585)
11-23 15:39:37.472: E/AndroidRuntime(2281): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:326)
11-23 15:39:37.472: E/AndroidRuntime(2281): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
11-23 15:39:37.472: E/AndroidRuntime(2281): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
11-23 15:39:37.472: E/AndroidRuntime(2281): at android.view.Window$LocalWindowManager.addView(Window.java:547)
11-23 15:39:37.472: E/AndroidRuntime(2281): at android.app.Dialog.show(Dialog.java:277)
11-23 15:39:37.472: E/AndroidRuntime(2281): at on.boot.completed.OnTextChanged.onPrimaryClipChanged(OnTextChanged.java:39)
11-23 15:39:37.472: E/AndroidRuntime(2281): at android.content.ClipboardManager.reportPrimaryClipChanged(ClipboardManager.java:230)
11-23 15:39:37.472: E/AndroidRuntime(2281): at android.content.ClipboardManager$2.handleMessage(ClipboardManager.java:75)
11-23 15:39:37.472: E/AndroidRuntime(2281): at android.os.Handler.dispatchMessage(Handler.java:99)
11-23 15:39:37.472: E/AndroidRuntime(2281): at android.os.Looper.loop(Looper.java:137)
11-23 15:39:37.472: E/AndroidRuntime(2281): at android.app.ActivityThread.main(ActivityThread.java:4745)
11-23 15:39:37.472: E/AndroidRuntime(2281): at java.lang.reflect.Method.invokeNative(Native Method)
11-23 15:39:37.472: E/AndroidRuntime(2281): at java.lang.reflect.Method.invoke(Method.java:511)
11-23 15:39:37.472: E/AndroidRuntime(2281): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-23 15:39:37.472: E/AndroidRuntime(2281): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-23 15:39:37.472: E/AndroidRuntime(2281): at dalvik.system.NativeStart.main(Native Method)
Note that iam using Emulator to test m program, Iam not using real device