I have already created a QR scanner android application. What I need is that if I scan a QR code and if it generates a link, so the app should automatically open that link through the browser.
Any help will be appreciated. Thank You!!
When you get the callback of QR code generated
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(browserIntent);
To open that link in browser
As for the missing "http://" I'd just do something like this:
if (!url.startsWith("http://") && !url.startsWith("https://"))
url = "http://" + url;
To validate the URL
URLUtil.isValidUrl(url)
If you have already implemented a "QR scanner" then check, there must be a callback method where you receive the returned text from the "Scanner".
Inside that callback method, fire an intent to open the browser with a specific url like shown below:
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url)); // here ulr is the one that you get from scanner
startActivity(i);
I hope it helps.
Down below is my QR scanner code :
public class VehicleReaderActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {
private ZXingScannerView mScannerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("SCAN QR CODE");
mScannerView = new ZXingScannerView(this);
setContentView(mScannerView);
mScannerView.setResultHandler(this);
mScannerView.startCamera();
}
#Override
protected void onPause() {
super.onPause();
mScannerView.stopCamera();
}
#Override
public void handleResult(Result result) {
// Do anything with result here
Log.w("handleResult", result.getText());
// Now direct the generated link to the browser //
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://" + result.getText()));
startActivity(browserIntent);
//Resume scanning
//mScannerView.resumeCameraPreview(this);
}
}
Related
I am trying to send the value stored in a variable my_var from one activity to the other in Android. There are probably already many similar questions here at SOV, but I have been trying things by my own, and so far, no success. I shall highly appreciate little help or hints on what I am doing wrong?
My (pseudo/example) code is like this:
public class MyActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
public String my_var;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
My_method();
}
public void my_method() {
// This is a method that makes HTTP GET request and parse response to my_var
my_var = responseObject1.getX() + " " + responseObject1.getY()
}
// Then, at the bottom of MyActivity, I am creating an Intent to pass my_var to another activity to show it in TextView.
// I took this method from here[.][1]
public void rsa_key(String s){
Intent intent = new Intent(getBaseContext(), AnotherClass.class);
intent.putExtra("my_var", my_var);
startActivity(intent);
}
}
Then, in the other activity (in OnCreate), I am trying to get my_var like this:
// public String my_var in initialization
Intent intent = getIntent();
my_var = intent.getStringExtra("my_var");
The app compiles, and I get no errors, but I can't see my_var value (XML layout) when put it in TextView.setText(my_var); in the other activity. There were no useful hints in the log as well. Can somebody help me to understand what am I doing wrong? or missing something.
I also tried SharedPreferences like this but no luck!
In first acitvity:
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("varKey", my_var);
editor.commit();
Second actvity:
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
my_var = sharedPref.getString("varKey", my_var);
I shall highly appreciate help/suggestions to fix this. Many thanks!
try this code for putExtra and get data in the second activity.
Use this to "put" the file...
Intent i = new Intent(FirstScreen.this, SecondScreen.class);
i.putExtra("my_var", my_var);
startActivity(i);
Then, to retrieve the value try something like:
String newString;
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if(extras == null) {
newString= null;
} else {
newString= extras.getString("my_var");
}
} else {
newString= (String) savedInstanceState.getSerializable("my_var");
}
I'm making an app where it uses intent to send data to another app. In case, the other app, which is supposed to receive data from my app, is not installed on users's device then it redirects user to play store with toast message asking user to install it. I used "if else" to achieve this. It worked all good until I found that if the other app is disabled by user (OEM installed app which can't be uninstalled), then my app crashes. In such a condition, I want to let user know that the app is disabled by them and ask them to enable it (through toast message). How can I achieve this?
Here is my complete code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Creates button view which is connected to a view in the XML layout, which gets triggered on touching the view.
TextView textView = (TextView) findViewById(R.id.location);
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Use package name which we want to check
boolean isAppInstalled = appInstalledOrNot("com.google.android.apps.maps");
if(isAppInstalled){
Uri gmmIntentUri = Uri.parse("geo:00,0000,00,0000");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);
} else {
Uri uri2 = Uri.parse("market://details?id=com.google.android.apps.maps");
Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri2);
Toast.makeText(MainActivity.this, "Google Maps not Installed", Toast.LENGTH_SHORT).show();
startActivity(goToMarket);
}
}
});
}
private boolean appInstalledOrNot(String uri) {
PackageManager pm = getPackageManager();
try {
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
return true;
} catch (PackageManager.NameNotFoundException e) {
}
return false;
}
}
I'm not sure if this will work for you, but since you know the package name, you could try this to do a check beforehand.
I'm creating an app to open a PDF application from a button, but what I did is not going well.
In my layout there is only one button , which seeks want and open a PDF file.
Not giving error in Eclipse , just in tablet appears " readerPDF stopped ."
I do not know what to do. somebody help me.
Sorry for my english.
public class ReaderActivity extends Activity {
private Button btnOpen;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reader);
btnOpen.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
String filename = null;
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +"/"+ filename);
Intent target = new Intent(Intent.ACTION_VIEW);
target.setDataAndType(Uri.fromFile(file),"application/pdf");
target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
Intent intent = Intent.createChooser(target, "Open File");
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
}
}
});
}
}
filename is null, so the path you are passing to the third-party app does not point to a PDF file.
Im brand new to android and I have no idea what I'm doing wrong. The current code looks like:
public class TypeActivity extends Activity {
private boolean alcoholin = false;
...
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.search_type);
alcohol = (Button) findViewById(R.id.alcohol_button);
...
alcohol.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alcoholin=true;
Intent i = new Intent (TypeActivity.this,ingredients.class);
startActivity(i);
}
});
...
public boolean getalcholin(){
return alcoholin;
}
This code is then supposed to set a value in another class. I have tested the code and I know that if i state the the boolean is true in the beginning of my code, then I will make the other code's boolean equal true. However, if I try to set the value when the user presses the button the value does not get updated.
Please help!
On Android the standard way to send data from one Activity to another is by specifying "extras" on the Intent that you use to start a new activity.
You are already using an Intent in your onClick method to start your "ingredients" activity (Your code would be more readable if you named your activity something like IngredientsActivity instead) - you just need to add some "extras" to it.
Please read up on the training tutorial here, but without knowing what your ultimate goal is you probably want something like:
alcohol.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent (TypeActivity.this,IngredientsActivity.class);
i.putExtra(IngredientsActivity.EXTRA_INGREDIENT_TYPE, "alcohol");
startActivity(i);
}
});
... and then in IngredientsActivity you would have something like:
public static final String EXTRA_INGREDIENT_TYPE = "ingredient";
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ingredients);
String ingredientType = getIntent().getStringExtra(IngredientsActivity.EXTRA_INGREDIENT_TYPE);
}
This would never work because alcoholin would NOT exist as this is an entirely different Activity. Why don't you instead use the Extras of the intent to pass data between the two Activities.
Standards
Class names should always have each word capitalized like MyClassObject in java.
public class OpenPdf extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.OpenPdfButton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
File file = new File("/sdcard/example.pdf");
if (file.exists()) {
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
}
catch (ActivityNotFoundException e) {
Toast.makeText(OpenPdf.this,
"No Application Available to View PDF",
Toast.LENGTH_SHORT).show();
}
}
}
});
}
}
I have tried this above code but no output can been seen on emulator.Please help me to solve my problem of reading a PDF on emulator
I think the issue is in the Activity where you're trying to show the .pdf.
I haven't tried opening any .pdf files in any of my apps, but a cursory search says there is no native, easy way to do it. In other words, there is no Android class that makes it a snap to show your .pdf files - so you'll have to use a third party library, or roll your own.
See Render a PDF file using Java on Android