How to read USSD Message response in android - java

i want to store my balance in sqlite database i am successfully dial the USSD
String balance_check="*444";
String encodedHash = Uri.encode("#");
String ussd = balance_check + encodedHash;
startActivityForResult(new Intent("android.intent.action.CALL",Uri.parse("tel:" + ussd)), 1);
Here is onActivityResult function
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
String dd = data.toString();
Log.d("myussdmessage", data.toString());}}
but I cannot get the string exactly how to do it.I want to store the balance form the dialogue to my database.Can any one suggest me how to do it.

You need to build an AccessibilityService in order to do that you need to read the documentation of AccessibilityService in below link
https://developer.android.com/reference/android/accessibilityservice/AccessibilityService.html
and also this question helped me
Prevent USSD dialog and read USSD response?

Related

Send output of android app on localserver

I am building an application using android speech recognition intent. I have build the application, now I want to send the recognized outputs to local server so that I can view the output of the recognizer from any device connected to the same network. Below is my code:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQ_CODE_SPEECH_INPUT: {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
mTextFromSpeech.setText(result.get(0));
String finalResult = result.get(0);
System.out.println("Detected Command Is: " + finalResult);
}
break;
}
}
}
I want to send the result from finalresult on local server, please help.

Twitter Android-FirebaseUI auth: How to post on user's behalf once I have the access Token and the access secret on Android App??are those enough?

I logged in successfully and got the twitter access token and the twitter access secret using firebase-ui-auth [https://github.com/firebase/FirebaseUI-Android/blob/master/auth/README.md][1]:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SIGN_IN && resultCode == RESULT_OK) {
IdpResponse response = IdpResponse.fromResultIntent(data);
//twitter access token
response.getIdpToken();
//twitter access token secret
response.getIdpSecret();
}
}
I want to post on user's behalf(to their accounts, not to mine) using these two tokens that I will save on shared preferences.
1) Are these two tokens enough to post to user's account?
2) How do I do to post something using these two tokens?. I can't seem to find the proper docs for my particular case, the twitter api handling for android is really poor.
I already solved it myself by using fabric and its TweetComposer class.....
first you need to initialize fabric on the bootstrap Class of your app
Fabric.with(this, new Twitter(authConfig));
then on the class you want to make the tweet you get the firebase instance to get the logged in user and then you set the twitter consumer key and secret that you got when you log in to firebase UI https://github.com/firebase/FirebaseUI-Android/blob/master/auth/README.md, for future reference to get the two tokens needed to tweet on user's behalf you can do it like the link specifies:
To retrieve the ID token that the IDP returned, you can extract an IdpResponse from the result Intent.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
IdpResponse idpResponse = IdpResponse.fromResultIntent(data);
startActivity(new Intent(this, WelcomeBackActivity.class)
.putExtra("my_token", idpResponse.getIdpToken()));
}
}
Twitter also returns an AuthToken Secret which can be accessed with idpResponse.getIdpSecret().
and now you have everything you need:
mAuth = FirebaseAuth.getInstance();
if (mAuth.getCurrentUser() != null) {
// already signed in
twitter_consumer_key= preferences.getString("TWITTER_CONSUMER_KEY","");
twitter_consumer_secret= preferences.getString("TWITTER_CONSUMER_SECRET","");
TwitterAuthConfig authConfig = new TwitterAuthConfig(twitter_consumer_key, twitter_consumer_secret);
//setting up fabric
Fabric.with(this, new TwitterCore(authConfig), new TweetComposer());
}
and then let's say I want to tweet from a custom button onClick:
ImageButton tweetButton= (ImageButton) findViewById(R.id.tweet_button);
tweetButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TweetComposer.Builder builder = new TweetComposer.Builder(mContext)
.text("just setting up my Fabric.");
builder.show();
}
});
the app will redirect you to the twitter app with the preseted message "just setting up my Fabric.". You can add pictures and videos too!
Hope that this helps someone in the future cause there is little info about fabric....

retrieve value from second activity

In my MainActivity, I launch a second activity:
Button button = (Button) findViewById(R.id.btnPush);
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
Intent nowStart = new Intent(getApplicationContext(), AddPillScheduleActivity.class);
startActivityForResult(nowStart, RESULT_OK);
//startSecond();
}
});
Then inside my Second Activity, I would like to return a value back to the main activity.
Intent i=new Intent();
i.putExtra("ANSWER", ans);
setResult(RESULT_OK,i);
finish();
That seems to execute fine, but back in my MainActivity, I would like to grab the value. This is where I am having trouble. My debugger never stops on my onActivityResult, which is:
#Override
protected void onActivityResult(int requestCode ,int resultCode ,Intent data ) {
super.onActivityResult(requestCode, resultCode, data);
String name = getIntent().getExtras().getString("ANSWER");
if (resultCode == RESULT_OK) {
Toast.makeText(this, name, Toast.LENGTH_LONG).show();
}
Can someone shed a little light? Thanks.
You're not getting the value from the right intent, the one that comes with the method. Modify your code to the following:
#Override
protected void onActivityResult(int requestCode ,int resultCode ,Intent data ) {
super.onActivityResult(requestCode, resultCode, data);
String name = data.getStringExtra("ANSWER");
if (resultCode == RESULT_OK) {
Toast.makeText(this, name, Toast.LENGTH_LONG).show();
}
}
Use the getStringExtra method as you put a String in your intent and not a bundle. Also, not the best practice use the same code for the requestCode and resultCode.
String name = getIntent().getExtras().getString("ANSWER");
is accessing the wrong intent. getIntent() returns the intent that started the activity. You want
String name = data.getExtras().getString("ANSWER");
Also, the second parameter of startActivityForResult() is a request code, so using RESULT_OK -- althought it still works -- is confusing.

integrating with intents for ocr and zxing

I have an app, i used this code to integrate zxing
public Button.OnClickListener mScan = new Button.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
}
};
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents = intent.getStringExtra("SCAN_RESULT");
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
// Handle successful scan
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
I have both zxing scanner as well as google goggles installed in my mobile phone. When i start the app and try to scan, I get the option to choose either the barcode scanner or the goggle app. I thought, hey let's try and use the goggle app for doing other stuff as well like OCR. I select the goggle option but the app does not have the take picture option within it. How do I integrate goggles also with my app? with full functionality?
I have no idea about how to integrate your app with Google Goggles. However, if you looking for an app that provide OCR function, you may use my app:
https://play.google.com/store/apps/details?id=sunbulmh.ocr
Here is a sample code that you can use in your app to get OCR service:
PackageManager pm = getPackageManager();
try {
pm.getPackageInfo("sunbulmh.ocr", PackageManager.GET_ACTIVITIES);
Intent LaunchIntent = pm.getLaunchIntentForPackage("sunbulmh.ocr");
LaunchIntent.setFlags(0);
startActivityForResult(LaunchIntent,5);
} catch (NameNotFoundException e) {
Uri URLURI = Uri.parse("http://play.google.com/store/apps/details?id=sunbulmh.ocr");
Intent intent = new Intent(Intent.ACTION_VIEW,URLURI);
startActivity(intent);
}
Then, get the result in onActivityResult():
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if(requestCode == 5){
String ocr_txt = data.getStringExtra(Intent.EXTRA_TEXT);
// ocr_txt contains the recognized text.
}
}
}

Responding to startActivityForResult

I am trying to respond to a result from an activity using startActivityForResult. I am getting the expected result but am unsure how to respond to the result. What I'd like to do is call a different ArrayList (which is a resource) depending on the result.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
Bundle d = data.getExtras();
String oil = d.getString("oil");
Resources res = getResources();
String[] oil_info = res.getStringArray(R.array.OIL);
...
}
}
I'd like R.array.OIL to change in response to the result. i.e. if oil = 'BASIL' change R.array.OIL to R.array.BASIL. I thought about making a hashmap but couldn't figure out how to put R.array.OIL into a hashmap as you cannot put primitives in a hashmap. I am pretty new to android and java so I'm sure there is a better way to do this. Any help would be much appreciated. Thanks.
Use getIdentifier() for your requirement as below.
e.g. in array type 2 items are there as OIL and BASIL with different values.
you can access like R.array.OIL and R.array.BASIL.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
Bundle d = data.getExtras();
String oil = d.getString("oil");
Resources res = getResources();
int id = res.getIdentifier(oil, "array", getPackageName());
String[] oil_info = res.getStringArray(id);
...
}
}
In above code if string oil is "OIL" then id will become R.array.OIL and oil is "BASIL" then id will become R.array.BASIL
what you are trying is actually an attempt of permanent storage . this is not allowed for "Res" folder or "R.smthing.smthing" data's .
use shared preferences for primitive storage .
see whether you can save array through sharedPreferences else use Serialization or SQLite database .

Categories

Resources