In the code, Toast works well but the intent to share used under the toast crashes the app.Please help to use the intent(used for sharing the text copied for a particular package) perfectly as the text copied.
public class Clipboard extends Service {
private ClipboardManager mCM;
IBinder mBinder;
int mStartMode;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
mCM = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
mCM.addPrimaryClipChangedListener(new ClipboardManager.OnPrimaryClipChangedListener() {
@Override
public void onPrimaryClipChanged() {
String newClip = mCM.getText().toString();
Toast.makeText(getApplicationContext(), newClip.toString(), Toast.LENGTH_LONG).show();
Log.i("LOG", newClip.toString() + "");
//intent starts ,this crashes the app
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
sendIntent.setClassName("com.package","com.package.activity");
sendIntent.putExtra(Intent.EXTRA_TEXT, newClip.toString());
startActivity(sendIntent);
}
});
return mStartMode;
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
MainActivity:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startService(new Intent(this, Clipboard.class));
}}
i think you should add this line to your code.
public class Clipboard extends Service {
private ClipboardManager mCM;
IBinder mBinder;
int mStartMode;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
mCM = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
mCM.addPrimaryClipChangedListener(new ClipboardManager.OnPrimaryClipChangedListener() {
@Override
public void onPrimaryClipChanged() {
String newClip = mCM.getText().toString();
Toast.makeText(getApplicationContext(), newClip.toString(), Toast.LENGTH_LONG).show();
Log.i("LOG", newClip.toString() + "");
//intent starts ,this crashes the app
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
sendIntent.setClassName("com.package","com.package.activity");
sendIntent.putExtra(Intent.EXTRA_TEXT, newClip.toString());
**//this flag is missing i think**
sendIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(sendIntent);
}
});
return mStartMode;
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}}