how can I navigate to the applications page on my phone by a button click from my app? is it possible?
UPDATE
The question was not not ver clear earlier. I don't think it's possible. I have worked extensively on Android Launcher code and there's no standard or undocumented Intent to open that page directly.
Nova Launcher has a shortcut provider which lets you choose any activity. Simply call this shortcut provider from you code and select the activity you want, and then simple record the returned Intent.
Though this intent not be universal for all android versions. It will have package name and component name (and not an action string).
Use this code in an Activity in a test project. Make sure you have Nova launcher installed (you can uninstall it later on)
public void onTestButtonClicked(View v) {
startActivityForResult(Intent.createChooser(new Intent(Intent.ACTION_CREATE_SHORTCUT), "Select Activities"), 10);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 10 && resultCode == RESULT_OK) {
Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
Log.e("Intent", intent.toUri(Intent.URI_INTENT_SCHEME));
}
}
Related
When I am in a fragmentB, how to simulate a click on a button located in an activity in the stack.
LoginActivityA => fragmentB.
I want to simulate something like this:
LoginActivityA.btnClick() from the fragmentB
I tried to use this tickets with no success: Ticket1, Ticket2, Ticket3
Here is my code:
FragmentB:
//BEGIN TEST
String message="hello ";
Intent intent = new Intent(getActivity(), LoginActivity.class);
intent.putExtra("MESSAGE",message);
getActivity().setResult(2,intent);
getActivity().startActivityForResult(intent,2);
getActivity().finish();//finishing activity
//END TEST
LoginActivityA:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
// check if the request code is same as what is passed here it is 2
if(requestCode==2)
{
//do the things u wanted
}
}
I precise that I try not to get multiple LoginActivity and Fragment launched. I would like to get only one stack with LoginActivity => Fragment
Any ideas?
EDIT:
I tried this from this tichet with no success:
((LoginActivity)getActivity()).login();
I launched the activity function from the presented fragment with an error
"No acceptable module found. Local version is 0 and remote version is
0."
You can use an interface with an onClick method that is implemented by your activity and then in fragment B call the method onClick whenever you want and pass the activity to it.
Although I presume what you are doing is wrong and you should change your mind around it.
This is not a good practice, but if you want you can call your activity UI elements by
((Button) getActivity().findViewById(R.id.button_id)).performClick();
Always remember, Android is a well architect platform, if you are trying to do something which is difficult, probably you should not do it. Rethink about your your design patterns.
I made a QR reader APP for my school project, the app works very well but it has a little mistake. When I scan a QR code the app just shows me the text. However, when I created a QR code linked to "www.google.com" (a simple link), my app just shows me "www.google.com" and doesn't open it in the browser.
I use this video to make my app : https://youtu.be/Fe7F4Jx7rwo
He is a nice guy He said : "use intent to open it in a browser"
But as I said in previous posts : "in my school my teacher prefer to teach Visual Basic instead Java or C++" ... So I'm a 0 in Java or C++
Can anyone suggest what to do?
I suggest the following. When you get the text from the QR call the following code to either open the browser or show the text on the screen (your current implementation):
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
...
String text = resultCode.getContents();
if (Patterns.WEB_URL.matcher(text).matches()) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(text));
startActivity(browserIntent);
} else {
Toast.makeText(this, text, Toast.LENGTH_LONG).show();
}
}
I'm trying to build an app that takes a picture with the camera an sends it back to the main activity to display it in an ImageVew. I saw a tutorial that saves the image in the SD Card when the picture is taken. I'm able to save the file but I'm having difficulties getting the location of the stored image.
I think that storing the image in the SD Card is too much work since that image is not that important.
Is there a way to "save" the image I just took with the camera in a BitMap element? if so is it more efficient than storing it in the SD Card?
Here is my MainActivity.java:
public class MainActivity extends AppCompatActivity {
private Uri imgLocation;
ImageView mImageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button capture = (Button) findViewById(R.id.am_btn_camera);
capture.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
imgLocation = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "fname_" +
String.valueOf(System.currentTimeMillis()) + ".jpg"));
intent.putExtra(MediaStore.EXTRA_OUTPUT, imgLocation);
startActivityForResult(intent, 1);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == RESULT_OK) {
Log.e("URI",imgLocation.toString());
mImageView.setImageBitmap(BitmapFactory.decodeFile(imgLocation.toString()));
}
}}
It is better to just save it on the external/internal storage and then access it. It is even better to use a third-party library to handle all the image rendering and their lazy-loading like:
Glide
Picasso
Fresco
Most of these libraries will focus on handling resources from HTTP URLs, but they can still be used to load information from a local file or a Content Provider.
Handling images with Bitmaps might cause OutOfMemoryError easily as shown in this link. Although, in this case, since you are only using 1 Bitmap, there is not a lot to worry about.
It's almost always better to save it in the file system and pass the filepath within activities.
Android Bitmaps literally consume a lot of memory, and it's never a good idea to keep it in memory unless it's really required.
I know, it's a bit of a hassle to store it in the file system and pass the file reference, but it'll prevent a lot of unwanted outOfMemory errors later within your app.
Use Picasso. It will automatically cache the file for you on the filesystem.
If you need to retain that file on disk for longer periods, you must save it yourself.
I have written custom home screen app from which I am launching apps.
The main problem is the opened apps doesn't return to my home screen app, instead they go to the launcher home screen, even though I have set my home screen app as default home screen. I want these apps to return to my home screen. How can i do that?
If you check documentation for the getLaunchIntentForPackage(..) method you see
"Returns either a fully-qualified Intent that can be used to launch
the main activity in the package, or null if the package does not
contain such an activity. "
That's pretty self explaining.. your "app package name" is not correct, it might be for some apps but not for others.
try something like
PackageManager pManager = getPackageManager();
List<PackageInfo> packs = pManager.getInstalledPackages(PackageManager.GET_INSTALLED_PACKAGES);
for (PackageInfo pi : packs) {
if(pi.packageName.toLowerCase().contains("app package name") )
{
Intent intent = pManager.getLaunchIntentForPackage(pi.packageName);
if (intent != null)
startActivity(intent);
}
}
I have just made application using BarCode scanner (ZXing 1.7). User doesn't use bar code scanner on his phone, therefore I can't add external Bar Code scanner into my application. I have added ZXing sources for into my project, but I don't know how I can execute it without intents. Please, help me.
Update: or how can I make that external bar code scanner will be installed automatically with my application?
You can't install the external barcode scanner to be installed automatically. What you could do is to check if it is installed, and if not show a dialog asking the user wether they want to install it (this will take the user to the app market link).
If you want to avoid this, you can integrate directly the ZXing library but it requires more work. The barcode scanner app is open source so you can see how to do it from there.
If the zxing barcode scanner is installed in the mobile, its very easy:
Intent intent = new Intent(
"com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "PRODUCT_MODE");//for Qr code, its "QR_CODE_MODE" instead of "PRODUCT_MODE"
intent.putExtra("SAVE_HISTORY", false);//this stops saving ur barcode in barcode scanner app's history
startActivityForResult(intent, 0);
and in OnActivityResult:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents =
data.getStringExtra("SCAN_RESULT"); //this is the result
}
else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
If its not installed: u can put this code in try-catch block and catching the exception, u can do this:
Uri marketUri = Uri
.parse("market://details?id=com.google.zxing.client.android");
Intent marketIntent = new Intent(Intent.ACTION_VIEW,
marketUri);
startActivity(marketIntent);
So it redirects the app to android market and ur app continues running once if the barcode scanner is installed.
If u dont want to use the other app in ur app, U have to download zxing library and try using the classes from core.jar file(it is created using apache ant). Follow this tutorial to do that: http://code.google.com/p/zxing/wiki/GettingStarted
Just use the provided Intent-based integration code. It's very easy. It will send the user to Market to download the app. This is much better than trying to automatically install it for at least three reasons. First, I do not think users expect apps to install other apps and probably don't like it. Second it will only possibly work if the user has set the device to allow third-party apps from outside Market. Finally, you will be installing a potentially old version.