How to learn what is the "key" in Android Studio Bundle? - java

I'm using a library which is not up to date. (https://github.com/notsukamto/GFIPhotoPicker)
It has a onActivityResult function to get activity result. It returns an intent with this function
if (selection != null) {
intent.putExtra(EXTRA_SELECTION, new LinkedList<>(selection));
}
public static List<Uri> getSelection(Intent data) {
return data.getParcelableArrayListExtra(EXTRA_SELECTION);}
So my question is what is the key for this Parcelable and how I get that intent correctly?
(I tried "EXTRA_SELECTION" which is not working)
Bundle[
{com.github.potatodealer.gfiphotopicker.activity.extra.SELECTION=
[file:///storage/emulated/0/DCIM/Camera/IMG_20190114_072919.jpg,
file:///storage/emulated/0/DCIM/Camera/IMG_20190114_072904.jpg,
file:///storage/emulated/0/DCIM/Camera/IMG_20190114_072848.jpg],
com.github.potatodealer.gfiphotopicker.activity.extra.FACEBOOK_SELECTION=[],
com.github.potatodealer.gfiphotopicker.activity.extra.INSTAGRAM_SELECTION=[]
}
]

If you access this directory in the github link you provided, there will be a EXTRA_SELECTION constant in each of those activities.
For example, if we click on the FacebookPreviewActivity.java, we see:
private static final String EXTRA_SELECTION = FacebookPreviewActivity.class.getPackage().getName() + ".extra.SELECTION";

Related

Checking Azure connected Database onClick for login

So Azure spit the following code for me to insert into an activity (Android Studio is what I'm using)
Add the following line to the top of the .java file containing your launcher activity:
import com.microsoft.windowsazure.mobileservices.*;
Inside your activity, add a private variable
private MobileServiceClient mClient;
Add the following code the onCreate method of the activity:
mClient = new MobileServiceClient("https://pbbingo.azurewebsites.net", this);
Add a sample item class to your project::
public class ToDoItem{ public String id; public String Text;}
In the same activity where you defined mClient, add the following code:
ToDoItem item = new ToDoItem();
item.Text = "Don't text and drive";
mClient.getTable(ToDoItem.class).insert(item, new TableOperationCallback<item>(){
public void onCompleted(ToDoItem entity, Exception exception, ServiceFilter response)
{
if(exception == null){
//Insert Succeeded
} else {
//Insert Failed
}
}});
My goal is to create a login page. I understand that the above was probably offered up more with a ToList in mind. I just want to get the syntax correct today. The problem I think, is my basic class structure. I have created an OnClick Listener within my on create that gets the ID from a button in my layout. I don't need it checking for anything in the database until the button has been actually clicked to either login or register.
public class LoginClass extends AppCompatActivity{
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.MyLoginLayout);
MobileServiceClient mClient = null;
try {
mClient = new MobileServiceClient ("myAzureWebsite", "AzureKey", this);
} catch (MalformedURLException e) {
e.printStackTrace();
}
Button Attempt = (Button) findViewById (R.id.mySubmitButton);
final MobileServiceClient finalMClient = mClient; // finalized so I can use it later.
Attempt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View v) {
final View thisView = v;
final MyToDoItemClass item = new MyToDoItemClass();
In MyToDoItemClass I have two variables (Both String) Just left over from
the example of a ToDoList (they are String ID and String Text)
item.Text = "Filler";
item.ID = "Fill";
finalMClient.getTable(MyToDoItemClass.class).insert(new Table OperationCallback<item>() { //<--- I'm getting an error that the variable, item
is from an unknown class...
public void onCompleted (Item entity, Exception exception, ServiceFilterResponse response){
if(exception == null) {
Intent i = new Intent (LoginClass.this, MainActivity.class);
startActivity(i);
}else{
Toast.makeText(thisView.getContext(), "Failed", Toast.LENGTH_LONG).show();
}}
});
}
});
}}
The problem is with that the TableOperationCallback is saying that the item from MyToDoItemClass class is from an unknown class.
There are many issues in your code, as below.
According to the javadoc for class MobileServiceClient, there is not a method insert(TableOperationCallback<E> callback), so the code finalMClient.getTable(MyToDoItemClass.class).insert(new Table OperationCallback<item>() {...} is invalid.
The generics E in Table OperationCallback<E> means that you need to write a POJO class name instead of E, not an object variable name like item, so the correct code should be new Table OperationCallback<MyToDoItemClass>, please see the Oracle tutorial for Generics to know more details.
The figure below shows all methods insert of class MobileServiceClient. The bold word Deprecated under the method name means that you should not use it for developing on new project, it‘s only compatible for old project on the new version of Java SDK.
Please follow the offical tutorial to develop your app. Any concern, please feel free to let me know.

is there away to find out if an apk is installed and if so open app

hi guys some of my previous questions have been marked down so please be nice.
what i want to know is if there is a bit of code i can use that tell the user of my app that an apk is installed. then to open it within my app.
i have a listview, inside the list view is an list of available apps for download. i have worked out how to find out if the apk is there install instead of download. but i cant seam to figure out the installed bit.
ive tried this
public static boolean isPackageInstalled(Context context, String packageName) {
final PackageManager packageManager = context.getPackageManager();
Intent intent = packageManager.getLaunchIntentForPackage(packageName);
if (intent == null) {
return false;
}
List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
return list.size() > 0;
}
then using
ispackageinstalled();
but this asks for Context, and string
so i tried add this to constructor
Context shb;
private Static String Showbox = "com.tk.Showbox";
then tried
ispackageinstalled(shb,Showbox);
and the app just crashes lol im obviously writing something wrong. also i would like for the selection to turn red if it installed if possible. but getting the app to open would be a great help cheers guys
You need to pass in an actual Context to the method. Your Activity subclass that is hosting the ListView is a Context. Pass the Activity into your ispackageinstalled() method.
found this worked the way i wanted
private boolean isCallable(Intent intent) {
if (intent == null) {
return false;
}
List<ResolveInfo> list = getPackageManager().queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
return list.size() > 0;
}
then making an Intent with the package details like this
Intent AllcastOpen = getPackageManager().getLaunchIntentForPackage("com.koushikdutta.cast");
then an if statement
if (isCallable(AllcastOpen) == true) {
AllcastInstalled.equals(true);
startActivity(AllcastOpen);
the allcastInstalled is a seperate method that changes the color of the selection if its installed/downloaded depending

How to query for the default SpeechRecognizer

How to find out the ComponentName of the default system speech recognizer, i.e. the one that is returned when createSpeechRecognizer(Context context) is called? (Actually, I only need to find out which input languages it supports, so if there is an answer only to that, then I'd appreciate it as well.)
The framework solves this by
String serviceComponent = Settings.Secure.getString(mContext.getContentResolver(),
Settings.Secure.VOICE_RECOGNITION_SERVICE);
(See the source code of SpeechRecognizer.)
However, this solution does not seem to be available to a third party app.
However, this solution does not seem to be available to a third party app.
I assume you came to such conclusion because Settings.Secure.VOICE_RECOGNITION_SERVICE is not a public API. However, Settings.Secure.getString() requires name of the row to lookup in secure table for the second argument. So, you can simply provide the actual the name of the row you are looking for: "voice_recognition_service".
That's, you can use the same code from SpeechRecognizer with slight change:
String serviceComponent = Settings.Secure.getString(mContext.getContentResolver(),
"voice_recognition_service");
Hope this helps.
UPDATE (I misread the original question)
SpeechRecognizer isn't the thing doing the speech processing, the Intent you pass to SpeechRecognizer, however, is (via startListening(Intent intent)). That intent uses RecognizerIntent.ACTION_RECOGNIZE_SPEECH and, AFAIK, can be detected in the old-fashioned way.
To detect defaults, try resolving the Intent that you want the find the default for but with the PackageManager.MATCH_DEFAULT_ONLY set.
Untested code:
String detectDefaultSpeechRecognizer(Context context) {
final Intent speechIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
// 1: Try to find the default speech intent
final ResolveInfo defaultResolution = context.getPackageManager().resolveService(speechIntent, PackageManager.MATCH_DEFAULT_ONLY);
if (defaultResolution != null) {
final ActivityInfo activity = defaultResolution.activityInfo;
if (!activity.name.equals("com.android.internal.app.ResolverActivity")) {
//ResolverActivity was launched so there is no default speech recognizer
return "";
}
}
// 2: Try to find anything that we can launch speech recognition with. Pick up the first one that can.
final List<ResolveInfo> resolveInfoList = context.getPackageManager().queryIntentServices(speechIntent, PackageManager.MATCH_DEFAULT_ONLY);
if (!resolveInfoList.isEmpty()) {
speechIntent.setClassName(resolveInfoList.get(0).activityInfo.packageName, resolveInfoList.get(0).activityInfo.name);
return resolveInfoList.get(0).activityInfo.packageName;
}
return "";
}
OLD ANSWER
Check out GAST, it has a way to check if a language is supported in a speech recognizer.
https://github.com/gast-lib/gast-lib/blob/master/library/src/root/gast/speech/SpeechRecognizingActivity.java#L70
You could also try to manually check the <recognition-service> metadata tag.
http://developer.android.com/reference/android/speech/RecognitionService.html#SERVICE_META_DATA
If you only want to find out which input languages the default system speech recognizer supports (createSpeechRecognizer (Context context)), there is a more straightforward way to do it.
All you need to do is using a RecognizerIntent.getVoiceDetailsIntent intent that will check the default system speech recognizer languages:
Intent intent = RecognizerIntent.getVoiceDetailsIntent(getApplicationContext());
if (intent != null) {
ctx.sendOrderedBroadcast(intent, null, new BroadcastReceiver() {
#Override
public void onReceive(Context context, final Intent intent) {
Log.d(TAG,
"Receiving Supported Speech Recognition Languages broadcast "
+ intent);
final Bundle extra = getResultExtras(false);
if ((getResultCode() == Activity.RESULT_OK)
&& (extra != null)
&& (mHandler != null)
&& ((extra
.containsKey(RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES)) || (extra
.containsKey(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE)))) {
List<String> supportedLanguages = extra
.getStringArrayList(RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES);
String prefLang = extra
.getString(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE);
}
}
},
null, Activity.RESULT_OK, null, null);
}

Trying to call the JavaScript function from Java Source code

I am developing an Android App to read the barcode by using the Zxing Barcode reader Plugin.
In the plugin there is an object named window.plugins.barcodeScanner with which we encode/decode the barcode.
I don't wanna use HTML to invoke things instead want the below Javascript function to be called from Java [on click of the image- the below function would be invoked].
function scanCode(){
window.plugins.barcodeScanner.scan(
function(result){
alert("Scanned Code: " + result.text
+ ". Format: " + result.format
+ ". Cancelled: " + result.cancelled);
},
function(error){
alert("Scan failed: " + error);
}
);
}
Kindly let me know how to achieve this.
Assumptions:
You've already setup the LibararyProject from https://github.com/wildabeast/BarcodeScanner/tree/master/src/android.
Your Activity is not extending CordovaActivity but is extending Activity.
Your main goal is really just to use the scanner. You were just trying to find an easy/quick way to do so and thought the PG plugin may do the trick.
All you have to do is pull out the scan and onActivityResult methods and some of the helper strings from https://github.com/wildabeast/BarcodeScanner/blob/master/src/android/com/phonegap/plugins/barcodescanner/BarcodeScanner.java and put them in your activity. You'll need to replace the references to cordova with your own activity.
End result may look something like this:
public static final int REQUEST_CODE = 0x0ba7c0de;
private static final String SCAN_INTENT = "com.google.zxing.client.android.SCAN";
public void scan() {
Intent intentScan = new Intent(SCAN_INTENT);
intentScan.addCategory(Intent.CATEGORY_DEFAULT);
this.startActivityForResult(intentScan, REQUEST_CODE);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
String barcode = intent.getStringExtra("SCAN_RESULT");
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
//Do whatever you need with the barcode here
} else if (resultCode == Activity.RESULT_CANCELED) {
// handle a canceled scan
} else {
// throw an error or something
}
}
}
If that works for you, then you don't even need cordova as a dependancy.
You can invoke Javascript code from native side on Android using the sendJavascript function defined in CordovaWebView.
In your case you would do something like this. Considering that the you want to call the following function:
function scanSuccessCallback(result) {
//do something
}
on the native side in your plugin:
this.webView.sendJavascript("scanSuccessCallback('the result');");

Maximum length of Intent putExtra method? (Force close)

I need some help with debugging my application. First of all: In emulator and on some other devices my app is running fine. On my device I got a force close (without a force close message).
The "crash" happens if the Activity of the app is changed.
Here is some code of the MainActivity class. It just reads html content from a web page over webview. And no, it is NOT possible to do this over HttpRequest because I was not able to simulate the post request.
public class MainActivity extends Activity {
public final static String EXTRA_HTML = "com.example.com.test.HTML";
private WebView mWebView;
private ProgressDialog mDialog;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.webView1);
CookieSyncManager.createInstance(this);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookie();
mWebView.setBackgroundColor(0);
mWebView.setWebChromeClient(new WebChromeClient() {
public boolean onConsoleMessage(ConsoleMessage cmsg) {
if (cmsg.message().startsWith("MAGIC")) {
mDialog.cancel();
/*HashMap<String, String> message = new HashMap<String, String>();*/
String msg = cmsg.message().substring(5);
Intent intent = new Intent(MainActivity.this,
ReadDataActivity.class);
/*message.put("message", msg);*/
/*intent.putExtra(EXTRA_HTML, message);*/
intent.putExtra(EXTRA_HTML, msg);
startActivity(intent);
}
return false;
}
});
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setPluginState(PluginState.OFF);
mWebView.getSettings().setLoadsImagesAutomatically(false);
mWebView.getSettings().setBlockNetworkImage(true);
mWebView.getSettings().setAppCacheEnabled(true);
mWebView.getSettings().setSavePassword(true);
mWebView.getSettings()
.setCacheMode(WebSettings.LOAD_NORMAL);
mWebView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String address) {
if (address.indexOf("mySession") != -1) {
view.loadUrl("javascript:console.log('MAGIC'+document.getElementsByTagName('html')[0].innerHTML);");
}
});
mWebView.loadUrl("http://www.myurl.de");
}
So, in the onConsoleMessage() method I just pass the html code to another Activity class which read, parse and display the content.
The problem is now that at this point when the ReadDataActivity class should be loaded the application just close and go back to the home screen without any message or user dialog.
Is it possible that the html code which is passed as a string to the ReadDataActivity is to big? I also try to add the html code as a string in a HashMap but the problem is the same.
Some ideas what I can do to debug the problem? Maybe I should try to create a Parcelable object?
In the emulator everything is working fine.
As per my experience (sometime ago), you are able to parcel up to 1MB of data in a Bundle for IPC. This limit can be reduced if a lot of transactions are happening at a given time. Further information here.
In order to overcome this issue, I would suggest you to save your content on a temp file and pass the path/URI of your temp file to your second activity. Then in your second activity, read the contents out from file, perform your desired operations and finally delete that file.
If you want, you may also incorporate Shared_Preferences for this task - if you think handling files is cumbersome.
I did some research on the maximum amount of data you can transfer using an Intent. And it seems that the limit is nowhere near 1MB or 90KB, it's more like 500KB (tested on API 10, 16, 19 and 23).
I wrote a blog post about this topic, you can find it here: http://web.archive.org/web/20200217153215/http://neotechsoftware.com/blog/android-intent-size-limit
The size limit of Intent is still pretty low in Jelly Bean, which is somewhat lower than 1MB (around 90K), so you should always be cautious about your data length, even if your application targets only latest Android versions.
I have seen that by writing and reading from a file consists of less performance .
Then I have seen this solution : . So I am using this solution :
public class ExtendedDataHolder {
private static ExtendedDataHolder ourInstance = new ExtendedDataHolder();
private final Map<String, Object> extras = new HashMap<>();
private ExtendedDataHolder() {
}
public static ExtendedDataHolder getInstance() {
return ourInstance;
}
public void putExtra(String name, Object object) {
extras.put(name, object);
}
public Object getExtra(String name) {
return extras.get(name);
}
public boolean hasExtra(String name) {
return extras.containsKey(name);
}
public void clear() {
extras.clear();
}
}
Then in MainActivity I have called it like the following :
ExtendedDataHolder extras = ExtendedDataHolder.getInstance();
extras.putExtra("extra", new byte[1024 * 1024]);
extras.putExtra("other", "hello world");
startActivity(new Intent(MainActivity.this, DetailActivity.class));
and in DetailActivity
ExtendedDataHolder extras = ExtendedDataHolder.getInstance();
if (extras.hasExtra("other")) {
String other = (String) extras.getExtra("other");
}
The fixed size of 1MB is not only limited to intents. As Intents, Content Providers, Messenger, all system services like Telephone, Vibrator etc. utilize IPC infrastructure provider by Binder. Moreover the activity lifecycle callbacks also use this infrastructure.
1MB is the overall limit on all the binder transactions executed in the system at a particular moment.
In case there are lot of transactions happening when the intent is sent,it may fail even though extra data is not large.
http://codetheory.in/an-overview-of-android-binder-framework/
A little late to the game, but I just ran up against the same issue. Writing the data to file didn't really make sense performance-wise in my case, but I came across this in my search for answers:
http://developer.android.com/guide/faq/framework.html#3
Using a singleton is better for me as there's no need for disk IO. Better performance if the data doesn't need to be persisted.
Here's an example implementation:
public class DataResult {
private static DataResult instance;
private List<SomeObject> data = null;
protected DataResult() {
}
public static DataResult getInstance() {
if (instance == null) {
instance = new DataResult();
}
return instance;
}
public List<SomeObject> getData() { return data; }
public void setData(List<SomeObject> data) { this.data = data; }
}
Then you can set using this in one activity:
DataResult.getInstance().setData(data);
And get it in the other activity like this:
List<SomeObject> data = DataResult.getInstance().getData();
The Binder transaction buffer has a limited fixed size - 1Mb.
But the problem is that buffer shared by all transactions in progress for the process.
So try to keep your intent's data as small as possible every time.
The use of static String variable is good. If there is a need for the user to go back & forth between different pieces of HTML, you can also use LruCache like this:
static LruCache<String, String> mMemoryCache;
final int kiloByte = 1024;
.
.
final int maxMemoryKB = (int) (Runtime.getRuntime().maxMemory() / kiloByte);
// then choose how much you want to allocate for cache
final int cacheSizeKB = maxMemoryKB / 8;
.
.
mMemoryCache = new LruCache<String, String>(cacheSizeKB) {
//#Override
protected int sizeOf(String key, String value) {
try {
byte[] bytesUtf8 = value.getBytes("UTF-8");
return bytesUtf8.length / kiloByte;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return -1;
}
};
.
.
String cacheKey = generateUniqueString(key);
if (mMemoryCache.get(key) == null) {
mMemoryCache.put(cacheKey, yourContent);
}
Intent intent = new Intent(getApplicationContext(), ReadDataActivity.class);
intent.putExtra(EXTRA_HTML, cacheKey);
startActivity(intent);
Then on the ReadDataActivity side
Intent intent = getIntent();
String cacheKey = intent.getStringExtra(EXTRA_HTML);
String contentString = MainActivity.mMemoryCache.get(cacheKey);
doSomethingWith(contentString);
This idea came from here.
An alternative solution for passing large data between activities is to use a static field. In your case add this line to ReadDataActivity class
public static String msg;
Then you can use the static field msg within MainActivity class as follows
ReadDataActivity.msg = cmsg.message().substring(5);
And finally start your activity without extra put
Intent intent = new Intent(MainActivity.this, ReadDataActivity.class);
startActivity(intent);

Categories

Resources