How to force using zxing lib with only my application? - android

Ok lets say there are 3 different applications which are using zxing lib on the phone. Whenever I want to open zxing with my own application android asks me whether to complete action using app 1 or app 2 or my own app. How do I force it to run only through my app without any dialog? Is there any chance to do it?
EDIT
In Additional to CommonsWare, you can do that if you want to handle barcode result on the
other activity.
step 1: jump to method called handleDecode in Capture Activity. Add these lines after handleDecodeInternally(rawResult, resultHandler, barcode);
Intent intent = new Intent(getIntent().getAction());
intent.putExtra("SCAN_RESULT", rawResult.getText());
setResult(RESULT_OK,intent);
finish();
step 2: Do whatever want to do on the other activity's onActivityResult event.
PS: Thanks again to CommonsWare.

First, there is no "zxing lib". You are supposed to use the Barcode Scanner application, tying it into your application at the activity level, ideally using their IntentIntegrator code. Here is a sample application demonstrating this. The creators of ZXing specifically do not support or endorse baking the Barcode Scanner source code into another application.
However, given your symptoms, I have to assume that you are trying to add the Barcode Scanner source code to your own application.
You presumably have something like this in your manifest on the scanning activity's element:
<intent-filter >
<action android:name="com.google.zxing.client.android.SCAN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
You are not Barcode Scanner. Yet, this <intent-filter> claims that you are Barcode Scanner.
You need to remove this <intent-filter>, modify your copy of the Barcode Scanner source code to not require it, and then start up the scanning activity using the component-based Intent constructor (e.g., new Intent(this, ThisIsYourRevisedScanningActivity.class)).

Just include this,this has done the needed for me..
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.setPackage(getPackageName());
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);

Android does not allow you to set this on your own. Only a user can set the default application for an action. If on your phone, you want your app to handle that event, then check the Use as default box before selecting your app in the picker.
For security reasons, Android does not allow you to set your app as the default without user interaction as then a malicious app could tie itself as the default to various events.

Actually you need to remove intent-filter like CommonsWare said, so it must be as follows:
<activity
android:name="com.google.zxing.client.android.CaptureActivity"
android:screenOrientation="landscape"
android:configChanges="orientation|keyboardHidden"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
android:windowSoftInputMode="stateAlwaysHidden">
</activity>
and instead of calling via external intent you should call zxing like:
private final static int ACTION_ZXING_SCANNER = 0x0000c0de; //IntentIntegrator.REQUEST_CODE
private void startZxingScanner() {
final Intent intent = new Intent(this, com.google.zxing.client.android.CaptureActivity.class);
intent.setAction(Intents.Scan.ACTION);
startActivityForResult(intent, ACTION_ZXING_SCANNER);
}
and then process result in onActivityResult() using request code ACTION_ZXING_SCANNER. The import string if needed:
import com.google.zxing.client.android.Intents;
note: this works for me and I added zxing project as a lib to my project so here it is - the "zxing lib" :)

Related

Intent to start a navigation activity

In my application I have an option to start navigation to selected POI. Basically what I want is to launch a turn-by-turn navigator from my application. The thing is I don't know which (if any) navigator is installed.
So, the question is how to start an intent by showing a list of suitable activities for navigation to the user first, letting him choose which one he would like to use? Also would be nice to find a way to pass extra parameters to selected activity (this sounds like an issue to me, since different navigation apps use different names for their extras, I guess).
In case it's not clear: I'm looking for a way to DISPLAY A LIST OF SUITABLE APPLICATIONS FOR NAVIGATION WITH THE OPTION TO MAKE ONE DEFAULT.
EDIT: Find here the implementation http://datamoil.blogspot.com/2011/04/android-universal-intent-to-start.html
The bad news is, there isn't a standard Intent URI for navigation.
Yes, google.navigation URIs exist, and an app may choose to support it.
The best solution I can think of is to:
Explicitly check for known apps
Implicitly check for apps hooking google.navigation: and perhaps geo: (but then you also get map apps)
You can enumerate the possible implicit targets using PackageManage.queryIntentActivities
Try:
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("google.navigation:q=New+York+NY"));
startActivity(intent);
First I used in my onTap method inside the button's listener:
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("google.navigation:q=New+York+NY"));
mContext.startActivity(i);
Then in manifest simply use:
<activity android:name=".LaunchGPS" android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
This will open any Navigator App your phone has such as VZ Navigagtor, Google or whatever the phone is loaded with. Worked for me the first time perfectly. Hope this solves your problem.
I found more advantages using this code (just for to show available navigator apps)
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("geo:"));
if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
startActivityForResult(Intent.createChooser(intent, "Continues with:", CHOOSE_NAVIGATOR_ID);
} else {
// Handle failure...
}

using android dialer in 3rd party app

guys. I am trying to build a voip app for android. I want to make use of the built-in android phone dialer. Can you guys give me some reference to it. I have been googling with no luck. Thanks
What you need to do is setup an Intent filter on the Activity you want to make the call. You do this inside your AndroidManifest.xml file. Modify your activity definition to include this intent filter:
<intent-filter>
<action android:name="android.intent.action.CALL" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="tel" />
</intent-filter>
Note: there are some alternative ways to call people (which can be seeing in the AndroidManifest.xml of the source I linked bellow, however this is the main one
Adding this will give the user the option to use your app when making a call, and this can be set as the default app if the user wishes.
You can then get the phone number by adding something like this code to your onCreate() method of your activity:
final Intent i = getIntent();
final Uri phoneUri = i.getData();
phoneUri now contains tel:00000000000 and you can easily get the number out of the Uri object
If you have problems in to future take a look at the android source. I got these bits of code from the phone app source if you want to take a look.
This should open the dialer with new special permissions:
Intent i = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:0000000000"));
startActivity(i);
That should open the dialer with the required telephone number already inserted.

how can i use .apk file into my own application android?

In developer.android.com I read central feature of android is you can make use of elements of other applications in your own application.Iam interested in this line.
1)How can we use the .apk downloaded suppose from android market in to our application ?
2)How can we use our own created applicaton into newly created application?
please give me guidance on this and if possible sample snippet?
Regards,
Rajendar
I guess you mean that:
A central feature of Android is that
one application can make use of
elements of other applications
(provided those applications permit
it). For example, if your application
needs to display a scrolling list of
images and another application has
developed a suitable scroller and made
it available to others, you can call
upon that scroller to do the work,
rather than develop your own. Your
application doesn't incorporate the
code of the other application or link
to it. Rather, it simply starts up
that piece of the other application
when the need arises.
Two last sentences are crucial. And the link provides more information about it. But briefly: application author can write his code in a manner that it can be reusable for others. He/she can do that by putting "intent filters" into his/her application's AndroidManifest.xml . E.g. Google's Camera application (the one that provides camera functionality and image gallery as well - yeah, the application can "expose" many "entry points" = icons in the home screen) has activity definition (one of many) as follows:
<activity android:name="CropImage" android:process=":CropImage" android:configChanges="orientation|keyboardHidden" android:label="#string/crop_label">
<intent-filter android:label="#string/crop_label">
<action android:name="com.android.camera.action.CROP"/>
<data android:mimeType="image/*"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.ALTERNATIVE"/>
<category android:name="android.intent.category.SELECTED_ALTERNATIVE"/>
</intent-filter>
</activity>
That means that one can use it's cropping the image functionality by sending intent:
/* prepare intent - provide options that allow
Android to find functionality provider for you;
will match intent filter of Camera - for matching rules see:
http://developer.android.com/guide/topics/intents/intents-filters.html#ires */
Intent i = new Intent("com.android.camera.action.CROP");
i.addCategory(Intent.CATEGORY_DEFAULT);
i.setType("image/*");
/* put "input paramters" for the intent - called intent dependent */
i.putExtra("data", /*... Bitmap object ...*/);
i.putExtra( /*... other options, e.g. desired dimensions ...*/ );
/* "call desired functionality" */
startActivityForResult(i, /* code of return */ CROPPING_RESULT_CODE);
CROPPING_RESULT_CODE that one can define in one's Activity is used to distinguish which external activity returned (helpfull if one calls several external functions) and is provided in calling activity's onActivityResult() method which is called when "remote" application finishes - below an example:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case CROPPING_RESULT_CODE:
/* ... crop returned ... */
if (data != null) {
/* get cropped bitmap */
Bitmap bmp = data.getParcelableExtra("data");
/* ... and do what you want ... */
}
case ANOTHER_RESULT_CODE:
/* ... another external content ... */
}
}
Other options are using: other Services or ContentProviders.
If you have any more questions don't hesitate.
Android uses Intents to allow applications request work be done by software residing in other applications. See my answer to this question for more details on Intents and an example of how your application can ask the Browser application to display a webpage.

Launching external application from my app

I would like to launch an app the user selects from within my application. However, I'm not sure how I'd go about doing this. I've tried this:
Intent intent = new Intent();
intent.setAction(Contacts.Intents.SHOW_OR_CREATE_CONTACT);
startActivity(intent);
But this seems to throw an error and force close my application. I also tried adding:
<action android:name="Contacts.Intents.SHOW_OR_CREATE_CONTACT"/>
in the AndroidManifest file, but to no avail.
A look at Logcat shows that it's an "IOexception - no such file or directory". A couple of questions arise from this. I read through the Android docs and noticed that the Contact.Intents class is deprecated. However, it's successor, ContactContracts is aimed at API level 5 whereas I'm targeting API level 3. Could this be the problem? Also, I've hardcoded this application into the code. Is there a way to retrieve the intents of any application the user selects so that they can be launched?
You need to pass extra information into the intent to tell Android what you want to show or create. Otherwise Android doesn't know what activity to start and (presumably in your case) throws an ActivityNotFoundException.
For a contact, you use the generic Intent.ACTION_INSERT_OR_EDIT then use the MIME type of an individual contact (Contacts.People.CONTENT_ITEM_TYPE).
For example:
Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
intent.setType(People.CONTENT_ITEM_TYPE);
intent.putExtra(Contacts.Intents.Insert.PHONE, "+1234567890");
intent.putExtra(Contacts.Intents.Insert.PHONE_TYPE, Contacts.PhonesColumns.TYPE_MOBILE);
That will bring up the contacts app, prompting you to select an existing contact to add the phone number to, or to create a new contact.
You don't need to add anything special to your manifest to start external activities. Only if you were to directly manipulate the contacts ContentProvider would you need to add the appropriate CONTACT permissions to your manifest.
I use this code for that purpose:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClassName("com.android.settings", "com.android.settings.Settings");
startActivity(intent);
This will launch the Settings app, you can use these also:
intent.setClassName("com.android.music", "com.android.music.MediaPlaybackActivityStarter");
intent.setClassName("com.android.contacts", "com.android.contacts.DialtactsContactsEntryActivity");
intent.setClassName("com.android.contacts", "com.android.contacts.DialtactsActivity");
The first starts the default music app, the second the contacts, and the third the dialer.
Hope this helps.
You need to pass in valid arguments to the apps you start. A lot of apps expect the data URI and / or certain extras to be valid.
Please try the following code:
Intent intent = new Intent(Contacts.Intents.SHOW_OR_CREATE_CONTACT);
this.startActivity(intent);
(sorry if there is something wrong on the syntax, I dont have android in this computer)
And remove the action from the manifest. that is not needed.
The action method is used for something else.
For more info, please look at the android site: http://developer.android.com/reference/android/content/Intent.html
Daniel
The activity you are calling should appear not only in the Manifest for its own package, but in the Manifest for the CALLING package, too.

Letting a third party app start my activity directly?

I'm working on an activity which other 3rd parties want to use in their own apps, via intents.
Right now this activity is catching urls via an intent filter, like this:
<activity android:name=".MyActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW"></action>
<category android:name="android.intent.category.DEFAULT"></category>
<category android:name="android.intent.category.BROWSABLE"></category>
<data android:host="www.mysite.com" android:pathPrefix="/test/" android:scheme="http"></data>
</intent-filter>
</activity>
The above works, whenever a user clicks a link in my app like:
"mysite.com/test/blah.html"
my app comes up as a choice, along with the browser, to open the link.
Now if a third party wants to use my app, I think they can use the above like this:
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://mysite.com/test/somedata"));
startActivity(intent);
While this would work, this probably won't give them the desired effect of jumping directly to my activity from theirs - the android chooser dialog will appear, asking if they want to open the intent data with the browser, or my app.
How can I let 3rd parties call my activity directly without broadcasting the intent like this? I'd like to make them still pass the same exact data scheme to me, but just let them open my activity directly.
Thank you
You would most likely need for them to call your activity directly
Class yourClass = Class.forName("com.yourdomain.yourapp.YourClass");
Intent intent = new Intent(this, yourClass);
If they don't have a jar to link against. Otherwise, they could just use
Intent intent = new Intent(this, YourClass.class);
And then put some extras in there. The whole concept of the browsable intent (along with the others) is to provide users with a choice of how they would like to view/use something. This is similar to what happens when you click "share" from the media viewer. The whole concept is to give them choice. If somebody wants to just start your activity, they will need to explicitly call it.
Edit: My reflection example above won't directly work unless the Dalvik class loader knows about your class (which it probably won't). You will actually need to specifically tell the VM to load a class from a foreign package. You can do that with the following code
Context foreignContext = createPackageContext("com.yourdomain.yourapp", Context.CONTEXT_IGNORE_SECURITY | Context.CONTEXT_INCLUDE_CODE);
Class<?> yourClass = foreignContext.getClassLoader().loadClass("com.yourdomain.yourapp.YourClass");
Now that they have the class object, they can then fire the intent like before. So the complete code is something like
Context foreignContext = createPackageContext("com.yourdomain.yourapp", Context.CONTEXT_IGNORE_SECURITY | Context.CONTEXT_INCLUDE_CODE);
Class<?> yourClass = foreignContext.getClassLoader().loadClass("com.yourdomain.yourapp.YourClass");
Intent intent = new Intent(this, yourClass);
startActivity(intent);
The solution described above by Chris Thompson didn't work for me. This one did: Android: Starting An Activity For A Different Third Party App
Just in case anyone runs into the same problem as I did.

Categories

Resources