简体   繁体   中英

intent.getAction() and intent.getType() returns null

I am trying to receive data from other apps when share button is pressed. App is shown in the chooser and when I press the app, it opens but I can't get text!!

Here's my splash screen if it makes any sense.

Cover.java

public class Cover extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    startActivity(new Intent(Cover.this,MainActivity.class));
    this.finish();
}
}

MainActivity.java

onCreate(...)
setContentView(....)
 Intent intent = getIntent();
    String action = intent.getAction();
    String type = intent.getType();
    Log.d("nikesh"," "+action); //this  prints null
    Log.d("nikesh"," "+type); //this prints null
    if (Intent.ACTION_SEND.equals(action) && type != null) {
        if ("text/plain".equals(type)) {
            handleSendText(intent);
        }
    }


   private void handleSendText(Intent intent) {
    String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
    Log.d("khee",sharedText);      //these are 
    if (sharedText != null) {      //not printed
        Log.d("khee",sharedText);
textView.setText(sharedText);
        // Update UI to reflect text being shared
    }
}

manifest.xml

 <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
            <action android:name="android.intent.action.SEND" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="text/plain" />
 </intent-filter>

You are using an explicit intent, which doesn't get resolved by the system and IntentFilters.

startActivity(new Intent(Cover.this, MainActivity.class));

If you still want to start the activity using the class, you will have to call setAction on the Intent.

Intent intent = new Intent(Cover.this,MainActivity.class);
intent.setAction("android.intent.action.SEND");
startActivity(intent);

or disregard the explicit part, and just set the action

Intent intent = new Intent();
intent.setAction("android.intent.action.SEND");
startActivity(intent);

I did this in the splash screen. txt is the key here.

    Intent intent = new Intent(Cover.this,MainActivity.class);
    intent.setAction("android.intent.action.SEND");
    intent.setType("text/plain");
    intent.putExtra("txt",getIntent().getStringExtra(Intent.EXTRA_TEXT));
    startActivity(intent);
    this.finish();

MainActivity.java

    Intent intent = getActivity().getIntent();
    String action = intent.getAction();
    String type = intent.getType();

   if (Intent.ACTION_SEND.equals(action) && type != null) {
        if ("text/plain".equals(type)) {
            handleSendText(intent);
        }
    }


  }
    public void handleSendText(Intent intent) {
    String sharedText =   getActivity().getIntent().getExtras().getString("txt");
    if (sharedText != null) {
        textView.setText(sharedText);
    }
}

Thank you so much @Robert Estivill to make me find problems!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM