在代码中, Toast 运行良好,但在toast下使用的共享意图会崩溃app.Please帮助使用intent(用于共享复制的文本)对于特定包装)完全如文本复制。
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));
}}
答案 0 :(得分:0)
我认为问题出在下面一行
sendIntent.setClassName("com.package","com.package.activity");
尝试对其进行评论或提供适当的活动名称,并且不要忘记在清单中注册指定的活动名称并运行您的代码。
希望这会对你有帮助!
答案 1 :(得分:0)
我认为您应该将此行添加到您的代码中。
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;
}}