So I have been tasked to create an android application that scans a QR code of a WCF Service URL, then call that URL with a username attached as a query string. I have managed to scan the barcode and create the query string which returns a string list of other URLs as it should when used in a browser but I have no idea how to go through with getting that string list inside of android. The code for getting the query string URL is here:
public class MainActivity extends AppCompatActivity {
private EditText inputUsernameField;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inputUsernameField = (EditText) findViewById(R.id.inputUsernameField);
}
public void scanNowButtonClick(View view) {
new IntentIntegrator(this).initiateScan();
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanResult != null) {
String contents = intent.getStringExtra("SCAN_RESULT");
String username = String.valueOf(inputUsernameField.getText());
Uri.Builder b = Uri.parse(contents).buildUpon();
if (username != null) {
b.appendQueryParameter("username", username);
}else{
Toast.makeText(this, "Please input a Username and try again.", Toast.LENGTH_LONG).show();
}
String myUrl = b.build().toString();
Intent myIntent = new Intent(MainActivity.this, ImageActivity.class);
myIntent.putExtra("myUrl", myUrl); //Optional parameters
MainActivity.this.startActivity(myIntent);
}
}
}
The Query URL is http://cmsnet.cms.net.nz/imageservice/persehtmlservice.svc/getimages?site=pukekohe&username=test
The application then starts another activity where the string list of image URLs that should be returned by the WCF call will be shown.
Thanks
Related
This is my code from the main activity class named Account class wherein when I press the add entry button it will navigate to the next activity which is the AddEntry class
//ADD ENTRY BUTTON
accountBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(Account.this, AddEntry.class);
startActivityForResult(intent,REQUEST_CODE_ADD);
}
});
After it goes to the second activity which is the AddEntry class, a picture would be captured using the user's camera when the ImageView named entryPhoto is clicked
entryPhoto.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent takePhoto = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File tempImage = null;
try {
tempImage = createImage();
} catch (Exception e) {
e.printStackTrace();
}
if (tempImage != null){
Uri uriImage = FileProvider.getUriForFile(c,"com.example.login.fileprovider",
tempImage);
mCurrentPhotoUri = uriImage;
takePhoto.putExtra(MediaStore.EXTRA_OUTPUT,uriImage);
if (ContextCompat.checkSelfPermission(c, Manifest.permission.CAMERA)!= PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(AddEntry.this,
new String[]{Manifest.permission.CAMERA},
REQ_CODE_CAMERA);
}
else {
startActivityForResult(takePhoto,REQ_CODE_TAKE_PHOTO);
}
}
}
});
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQ_CODE_TAKE_PHOTO && resultCode == RESULT_OK) {
entryPhoto.setImageURI(mCurrentPhotoUri);
}
Here is the code where I put all the the information inputted by user then send it back to the main activity. In the addPhoto is where I am supposed to put the image to be passed. I only tried passing an image from my drawable because I really don't know how to pass the image captured.
Intent data = new Intent();
data.putExtra("addPhoto",R.drawable.anonymous);
data.putExtra("addName", entryName.getText().toString());
data.putExtra("addRemark", entryRemark.getText().toString());
data.putExtra("addBirthday", entryBirthday.getText().toString());
data.putExtra("addAddress", entryAddress.getText().toString());
data.putExtra("addGender", selectedGender);
data.putExtra("addContactNo", entryContactNo.getText().toString());
data.putExtra("addHobbies", entryHobbies.getText().toString());
data.putExtra("addOtherInfo", entryOtherInfo.getText().toString());
setResult(RESULT_OK,data);
finish();
And this is the code in the onActivityResult in the main activity
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data); //ONACTRESULT OF ADD ENTRY
if (requestCode == REQUEST_CODE_ADD && resultCode == RESULT_OK){
int addPhoto = data.getIntExtra("addPhoto",1);
String addName = data.getStringExtra("addName");
String addRemark = data.getStringExtra("addRemark");
String addBirthday = data.getStringExtra("addBirthday");
String addAddress = data.getStringExtra("addAddress");
String addContactNo = data.getStringExtra("addContactNo");
String addGender = data.getStringExtra("addGender");
String addHobbies = data.getStringExtra("addHobbies");
String addOtherInfo = data.getStringExtra("addOtherInfo");
entryList.add(0,new Entry(addPhoto,addName,addRemark,addBirthday,addGender,addAddress,addContactNo,addHobbies,addOtherInfo));
myAdapter.notifyDataSetChanged();
}
Please I hope you will help me. I am still a beginner but I really want to be good at programming. Thank you in advance.
I think you can pass the Uri directly in bundle of Intent as it extends Parcelable which means it can retrieved as is
Adding
data.putExtra("addPhoto", mCurrentPhotoUri);
Retrieving
Uri photoUri = getIntent().getParcelableExtra<Uri>("addPhoto");
im kinda new to programming and would like to make some sort of like a login and register page. so from my login page i click register and it would go to the register page and i want the username/password i get from the register page to be used in the previous login page to login into the app. But i cant seem to set the username/password using the result. Only able to set the textview. pls help heres the code
public class MainActivity extends AppCompatActivity {
private EditText Name;
private EditText Password;
private TextView Info;
private Button Login;
private int counter = 5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Name = (EditText)findViewById(R.id.etName);
Password = (EditText)findViewById(R.id.etPass);
Info = (TextView) findViewById(R.id.tvInfo);
Login = (Button)findViewById(R.id.btnLogin);
Info.setText("No Of Attempts Remaining: 5");
Login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
validate(Name.getText().toString(),Password.getText().toString());
}
});
}
private void validate(String userName, String userPassword){
if(userName.equals("") && userPassword.equals("")){
Intent intent = new Intent(MainActivity.this, MenuActivity.class);
startActivity(intent);
}
else{
counter--;
Info.setText("No Of Attempts Remaining: " + String.valueOf(counter));
if(counter == 0){
Login.setEnabled(false);
}
}
}
public void facebooklogin(View myview){
Intent intent = new Intent(this, MenuActivity.class);
startActivity(intent);
}
public void register(View myview) {
Intent i = new Intent(this, RegisterActivity .class);
startActivityForResult(i, 1);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if(resultCode == RESULT_OK) {
String reginame = data.getStringExtra("NAME");
String regipass = data.getStringExtra("PASS");
Name.setText("" + reginame);
Password.setText("" + regipass);
}
}
}
How do i set the
private void validate(String userName, String userPassword){
if(userName.equals("") && userPassword.equals("")){
Intent intent = new Intent(MainActivity.this, MenuActivity.class);
startActivity(intent);
}
to be equal to the onActivityResult reginame and regipass
your condition is wrong it should be like this:-
private void validate(String userName, String userPassword){
if(!userName.equals("") && !userPassword.equals("")){
Intent intent = new Intent(MainActivity.this, MenuActivity.class);
startActivity(intent);
}
First Activity.
Send information
Intent send = new Intent(MainActivity.this, Main2Activity.class);
send.putExtra("login",editText.getText());
send.putExtra("password",editText1.getText());
startActivity(send);
Second Activity.
Get Information
if(getIntent()!=null){
Intent intent = getIntent();
editText.setText(intent.getStringExtra("login")) ;
editText1.setText(intent.getStringExtra("password")) ;
}
MainActivity
public class MainActivity extends AppCompatActivity {
private static final int REQ_CODE_TO_ADD = 123;
final ArrayList<Contact> allContact = new ArrayList();
ArrayList<String> name = new ArrayList();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Intent intent = new Intent(this,DetailActivity.class);
Button addbt = (Button)findViewById(R.id.addbt);
public void onClickAdd(View v){
Intent intent = new Intent(this,AddContactActivity.class);
startActivityForResult(intent,REQ_CODE_TO_ADD);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQ_CODE_TO_ADD){
if(resultCode == 0){
Intent intent = getIntent();
String name2 = intent.getStringExtra("namev");
String email2 = intent.getStringExtra("emailv");
String birthday2 = intent.getStringExtra("birthdayv");
Log.d("AAA",">>>:"+name2);
Contact person = new Contact(name2,email2,birthday2);
allContact.add(person);
}}
}
}
AddContactActivity
public class AddContactActivity extends AppCompatActivity {
private static final int REQ_CODE_TO_MAIN = 321;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_contact);
}
public void onClickOk(View v){
EditText name = (EditText)findViewById(R.id.nameet);
EditText email = (EditText)findViewById(R.id.email);
EditText birthdate = (EditText)findViewById(R.id.birthdate);
Intent intent = new Intent();
intent.putExtra("namev",name.getText().toString());
intent.putExtra("emailv",email.getText().toString());
intent.putExtra("birthdayv",birthdate.getText().toString());
setResult(0,intent);
finish();
}
}
AddContactActivity I already use intent.putExtra name.getText().toString() and send intent to MainActivity
Why onActivityResult() in MainActivity Log.d output is null?
if(resultCode == 0){
//Intent intent = getIntent();
String name2 = data.getStringExtra("namev");
String email2 = data.getStringExtra("emailv");
String birthday2 = data.getStringExtra("birthdayv");
Log.d("AAA",">>>:"+name2);
Contact person = new Contact(name2,email2,birthday2);
allContact.add(person);
}}
you need to use the data not getIntent()
You are using the Intent which originally launched the Activity. Use the Intent which was sent as a parameter instead.
There's no need for
Intent intent = getIntent();
Intent is already passed as argument i.e 'data'
Use this variable to extract data.
Hope this helps.
Intent intent = getIntent(); // This line is wrong
String name2 = intent.getStringExtra("namev");
String email2 = intent.getStringExtra("emailv");
String birthday2 = intent.getStringExtra("birthdayv");
Modify your code like this
if(requestCode==2 && resultCode==RESULT_OK){
Bundle bundle=data.getExtras();// here "data" is your intent
String string=bundle.getString("message");
Log.i(TAG,"onActivityResult Called..."+string);
}
as the title says, I'm trying to scan 1D barcodes, so far I have thet following code:
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void test(View view){
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "1D_CODE_MODE");
startActivityForResult(intent, 0);
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
switch (requestCode) {
case IntentIntegrator.REQUEST_CODE:
if (resultCode == Activity.RESULT_OK) {
IntentResult intentResult =
IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (intentResult != null) {
String contents = intentResult.getContents();
String format = intentResult.getFormatName();
TextView uno = (TextView) findViewById(R.id.textView1);
uno.setText(contents);
Toast.makeText(this, "Numero: " + contents, Toast.LENGTH_LONG).show();
Log.d("SEARCH_EAN", "OK, EAN: " + contents + ", FORMAT: " + format);
} else {
Log.e("SEARCH_EAN", "IntentResult je NULL!");
}
} else if (resultCode == Activity.RESULT_CANCELED) {
Log.e("SEARCH_EAN", "CANCEL");
}
}
}
}
And of course, I have both IntentResult and IntentIntegrator added to the project.
So, the scanner is beeing called correctly when a button is pressed and it seems to scan the code perfectly (it says "Text found" after it scans it), but it seems that the onActivityResult is not called, since the TextView is not beeing updated and the Toast is not appearing.
Any idea on what the mistake could be?
Thanks in advance!
Your first mistake is not using IntentIntegrator.initiateScan(), replacing it with your own hand-rolled call to startActivityForResult().
Your second mistake is in assuming that IntentIntegrator.REQUEST_CODE is 0. It is not.
Hence, with your current code, you are sending out a request with request code of 0, which is coming back to onActivityResult() with request code of 0, which you are ignoring, because you are only looking for IntentIntegrator.REQUEST_CODE.
Simply replace the body of your test() method with a call to initiateScan(), and you should be in better shape. Here is a sample project that demonstrates the use of IntentIntegrator.
I resolve your same problem so.
public class MainActivity extends Activity {
private TextView tvStatus, tvResult;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.tvStatus = (TextView) findViewById(R.id.tvStatus);
this.tvResult = (TextView) findViewById(R.id.tvResult);
Button scanBtn = (Button) findViewById(R.id.btnScan);
scanBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
try {
Intent intent = new Intent(
"com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_FORMATS", "QR_CODE_MODE");
startActivityForResult(intent,
IntentIntegrator.REQUEST_CODE);
} catch (Exception e) {
Log.e("BARCODE_ERROR", e.getMessage());
}
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
IntentResult scanResult = IntentIntegrator.parseActivityResult(
requestCode, resultCode, intent);
if (scanResult != null) {
this.tvStatus.setText(scanResult.getContents());
this.tvResult.setText(scanResult.getFormatName());
}
}
}
The onActivityResault function must be overridden. just add an #Override before the function declaration and it will be solved.
I know some others have had this problem but I've followed the solutions and it still isn't working for me.
I've created a new application, it has 1 activity which has 1 button (scan button) and 2 textviews (which are just going to output the formatname and contents that Zxing returns at the moment).
I have followed the ScanningViaIntent tutorial but it doesn't seem to be hitting onActivityResult
Below is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final IntentIntegrator integrator = new IntentIntegrator(this);
Button btnScan = (Button) findViewById(R.id.button1);
btnScan.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
integrator.initiateScan();
}
});
}
public void OnActivityResult(int requestCode, int resultCode, Intent intent)
{
Log.i("result", "hit line");
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
TextView tv1 = (TextView) findViewById(R.id.textView1);
TextView tv2 = (TextView) findViewById(R.id.textView2);
if(scanResult != null)
{
System.out.println("format: " + scanResult.getFormatName());
System.out.println("contents: " + scanResult.getContents());
tv1.setText(scanResult.getFormatName());
tv2.setText(scanResult.getContents());
}
else
{
tv1.setText("ERROR");
}
}
TextView1 never says "Error" so it doesn't seem that scanResult is null and my Log.i() line is never hit so I'm thinking that onActivityResult isn't even being hit.
Could it be to do with making IntentIntegrator final for the OnClick() method? When I created IntentIntegrator inside OnClick(), I used getParent() to pass the Activity to the constructor but this force closed my app with a NullReferenceException inside IntentItegrator.
Am I using the library correctly?
Thanks for your time,
Poncho
You are not actually overriding the method onActivityResult() because you have implemented OnActivityResult(). Your method is not being called as a result. Everything else looks about right.
This is the kind of thing you catch if you use #Override annotations -- good habit, since it would have caught this.
Where are you calling startActivityForResult(..)? You may want to use something like this :
Intent intentScan = new Intent(BS_PACKAGE + ".SCAN");
intentScan.addCategory(Intent.CATEGORY_DEFAULT);
// check which types of codes to scan for
if (desiredBarcodeFormats != null) {
// set the desired barcode types
StringBuilder joinedByComma = new StringBuilder();
for (String format : desiredBarcodeFormats) {
if (joinedByComma.length() > 0) {
joinedByComma.append(',');
}
joinedByComma.append(format);
}
intentScan.putExtra("SCAN_FORMATS", joinedByComma.toString());
}
String targetAppPackage = findTargetAppPackage(intentScan);
if (targetAppPackage == null) {
return showDownloadDialog();
}
intentScan.setPackage(targetAppPackage);
intentScan.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intentScan.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
attachMoreExtras(intentScan);
startActivityForResult(intentScan, REQUEST_CODE);
findTargetAppPackage :
private String findTargetAppPackage(Intent intent) {
PackageManager pm = activity.getPackageManager();
List<ResolveInfo> availableApps = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
if (availableApps != null) {
for (ResolveInfo availableApp : availableApps) {
String packageName = availableApp.activityInfo.packageName;
if (targetApplications.contains(packageName)) {
return packageName;
}
}
}
return null;
}
To see a more complete example go here.
You need to get the latest classes from the repository
https://github.com/zxing/zxing/tree/master/android-integration/src/main/java/com/google/zxing/integration/android
See the javadoc of the class to see how to use it. First add code to invoke the Intent:
IntentIntegrator integrator = new IntentIntegrator(yourActivity);
integrator.initiateScan();
Second, add this to your Activity to handle the result:
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanResult != null) {
// handle scan result
}
// else continue with any other code you need in the method
}
More info here https://github.com/zxing/zxing/wiki/Scanning-Via-Intent