意图使应用程序崩溃,因为它在剪贴板服务中使用 - Android

Intent crashes the app as it was used in Clipboard service - Android

在代码中,Toast 运行良好,但在 toast 下使用的 intent to share 崩溃了 app.Please 帮助将意图(用于共享为特定包复制的文本)完美地用作复制的文本。

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;
   }
}

主要活动:

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));
}}

我认为问题出在下面这行

sendIntent.setClassName("com.package","com.package.activity");

尝试对其进行评论或提供适当的 activity 名称,并且不要忘记在清单中注册给定的 activity 名称和 运行 您的代码。

希望对您有所帮助!

我认为您应该将这一行添加到您的代码中。

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;
    }}