3

I am trying to use a library where I have to pass the action and the URI and to open the activity I have to use the package name but the thing is even I change the package name it is not working at all this is the code that I am trying

 Intent intent = new Intent(Intent.ACTION_EDIT, Uri.parse(filename));
        intent.putExtra("was_get_content_intent", mWasGetContentIntent);
        intent.setClassName("neelay.mediaplayer.beatbox.ringdroid", "neelay.mediaplayer.beatbox.ringdroid.RingdroidEditActivity");
        startActivityForResult(intent, REQUEST_CODE_EDIT);

and this is my manifest code for setting the activity

 <activity
        android:name=".ringdroid.RingdroidSelectActivity"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Holo">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.GET_CONTENT" />

            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.OPENABLE" />

            <data android:mimeType="audio/*" />
        </intent-filter>
    </activity>
    <activity
        android:name=".ringdroid.RingdroidEditActivity"
        android:configChanges="orientation|screenSize|keyboardHidden"
        android:hardwareAccelerated="true"
        android:windowSoftInputMode="adjustResize">
        <intent-filter android:label="@string/edit_intent">
            <action android:name="android.intent.action.EDIT" />

            <data android:mimeType="audio/*" />
        </intent-filter>
    </activity>

The package name is package="neelay.mediaplayer.beatbox" I know it is easy to open the activity by just calling the names of the activity but in this case i have to pass the action with a uri and this is the only possible way i can see and guide will be helpful .

SAVVY
  • 842
  • 16
  • 33
  • Please explain, in detail, what "it is not working" means. – CommonsWare Oct 31 '17 at 13:34
  • I mean I call this intent it just do nothing as there is no startActivity is called – SAVVY Oct 31 '17 at 13:36
  • Is the activity you want to start is in different application or same application? – Shivam Oct 31 '17 at 13:37
  • Its in same application in ringdroid package – SAVVY Oct 31 '17 at 13:39
  • "there is no startActivity is called" -- you are calling `startActivityForResult()` in the fourth line of your first code snippet. When you make that call, you will either start the desired activity or crash with an `ActivityNotFoundException`. Since this appears to be an activity in your own app, a safer way to construct the `Intent` would be `new Intent(this, RingdroidEditActivity.class).setAction(Intent.ACTION_EDIT).setDataAndType(Uri.parse(filename), ...).putExtra("was_get_content_intent", mWasGetContentIntent);`, where `...` is replaced by the actual MIME type of this content. – CommonsWare Oct 31 '17 at 13:41
  • @CommonsWare let me try it i will let you know – SAVVY Oct 31 '17 at 13:42
  • @CommonsWare I tried but it is still not able to open it ` Intent intent = new Intent(RingdroidSelectActivity.this, RingdroidEditActivity.class); intent.setAction(Intent.ACTION_EDIT); intent.setDataAndType(Uri.parse(filename),"media"); intent.putExtra("was_get_content_intent", mWasGetContentIntent); startActivity(intent);` – SAVVY Oct 31 '17 at 14:01
  • `media` is not a valid MIME type, and it does not match the `` element in your ``. You should know what the actual MIME type is, since it is your file. If for some reason you do not, use `setData()` instead of `setDataAndType()`, and Android will try to guess based on things like the file extension. Also, my guess is that you have your code wrapped in a `try`/`catch` block, and you need to ensure that it logs errors to LogCat, so you can see these errors when you are debugging your app. – CommonsWare Oct 31 '17 at 14:21
  • What is the value of `REQUEST_CODE_EDIT`? – David Wasser Nov 01 '17 at 16:17
  • @DavidWasser its is 1 – SAVVY Nov 01 '17 at 17:27
  • Check this one:: https://stackoverflow.com/questions/9923602/cannot-start-new-intent-by-setclassname-with-different-package-in-android – Sagar Chavada Nov 02 '17 at 07:22
  • you should add full package name in manifest like : neelay.mediaplayer.beatbox.ringdroid.RingdroidSelectActivity | and also try to add flag for where you are trying to open this activity.. is it from background. – Sagar Chavada Nov 02 '17 at 07:23

1 Answers1

1

You said your package name (in the manifest) is "neelay.mediaplayer.beatbox". In that case you need to change this:

intent.setClassName("neelay.mediaplayer.beatbox.ringdroid", "neelay.mediaplayer.beatbox.ringdroid.RingdroidEditActivity");

to this:

intent.setClassName("neelay.mediaplayer.beatbox", "neelay.mediaplayer.beatbox.ringdroid.RingdroidEditActivity");

Since there are a number of ways to set the Component, you can also use any of the following methods:

intent.setClassName(this, "neelay.mediaplayer.beatbox.ringdroid.RingdroidEditActivity");

or like this:

intent.setClassName(getApplicationContext(), "neelay.mediaplayer.beatbox.ringdroid.RingdroidEditActivity");

or like this:

intent.setComponent(new ComponentName("neelay.mediaplayer.beatbox", "neelay.mediaplayer.beatbox.ringdroid.RingdroidEditActivity"));

or like this:

intent.setComponent(new ComponentName(this, "neelay.mediaplayer.beatbox.ringdroid.RingdroidEditActivity"));

or like this:

intent.setComponent(new ComponentName(getApplicationContext(), "neelay.mediaplayer.beatbox.ringdroid.RingdroidEditActivity"));
David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • yes @david wasser you are right this is what i was making mistake but can you add one more thing intent.setClassName(getApplicationContext(), "neelay.mediaplayer.tryal.ringdroid.RingdroidEditActivity"); this one also worked for me then it will be a complete ans – SAVVY Nov 02 '17 at 18:49
  • 1
    There are several ways to set the component, you can use any of them. – David Wasser Nov 03 '17 at 11:44