Problem:
I am trying to write a barebones android application that just shows your advertising ID in a textview. My code compiles fine but when I run it my textview remains unchanged (it just remains as "New Text").
This is a picture of the screen I'm getting on both my AVD and Actual Device
My Code:
I have already imported the google play services SDK and am using this following code within the onCreate method which is within the MainActivity class.
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//The textview I am trying to change
uniqueID = (TextView) findViewById(R.id.special);
// declarations for included ad plugin
adtechView = (AdtechBannerView) findViewById(R.id.ad_container);
adtechView.getAdConfiguration();
Context mContext = MainActivity.this.getApplicationContext();
try {
//This should be getting the advertising ID
AdvertisingIdClient.Info adInfo = AdvertisingIdClient.getAdvertisingIdInfo(mContext);
String AdId = adInfo.getId();
//This should change the textview "uniqueID" to the Ad ID
uniqueID.setText("Your ID is " + AdId);
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (GooglePlayServicesRepairableException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
What I've tried:
I have looked into the problem through some of related questions. multiple sources say that the code might not work due to not having access to google play services.
I have tried running the code both on my AVD which does not have google play services and on my actual device which does. Both end up with the same result. (textview remains unchanged)
I have written this code based on this website: https://www.safaribooksonline.com/blog/2014/01/16/advertising-id-android-kitkat/
which provides a tutorial for retrieving the Ad-ID.
I have also used this stack overflow as guide, but I am not face with the same issue his code was facing:
AdvertisingIdClient getAdvertisingIdInfo hangs forever
What am I doing wrong? Thanks so much in advance!
You can't get the Advertiser ID from the main thread. https://www.safaribooksonline.com/blog/2014/01/16/advertising-id-android-kitkat/
From the code above, it appears that this is taking place on the main thread. I'd recommend using an AsyncTask. Put the advertising ID in the doInBackground implementation. Since you can only modify UI on the main thread, set the text of the TextView in the onPostExecute implementation.
Related
I am sharing data between two my app, this is my code for get the data from the shared pref in app A
try {
final Context mContext = createPackageContext("com.example.demo", Context.MODE_PRIVATE);
final String val = mContext.getSharedPreferences("pref_name",Context.MODE_PRIVATE).getString(MY_KEY,"");
Log.e("sharedtest",val);
finish();
} catch (Exception e) {
e.printStackTrace();
}
this code is inside the onCreate() method, I don't have any more code anywhere. My problem is that, if I save the some value in my app B and than start my app A the saved data were correctly retrieved at first time, after retrieving the data my activity were finishing (I have only one activity), and if I start my launcher icon and start my app A, there is no updated data(it is the same), which where changed from app B.
also if I kill my app from system app settings and launch it like first time launch updated data is here, every data change needs my app killing from settings, how can I fix that? what I'm missing?
I found solution it may be a trick but it works fine for me,
after my app A finishes its job, I'm calling the system exit method.
System.exit(1);
it makes app "A" to be exit and finish job completely
After that I had the latest updated data in my preferences
Im creating an android app where im reusing the google places api to get the autocomplete function. I way i have succesfully implemented functionality is by using this code.
int PLACE_AUTOCOMPLETE_REQUEST_CODE = 1;
...
try {
Intent intent =
new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_OVERLAY)
.build(this);
startActivityForResult(intent, PLACE_AUTOCOMPLETE_REQUEST_CODE);
} catch (GooglePlayServicesRepairableException e) {
// TODO: Handle the error.
} catch (GooglePlayServicesNotAvailableException e) {
// TODO: Handle the error.
}
This code is launched when the user clicks on a menu_button from a popupmenu in the firstscreen (mainscreen)
This code is able to do what i want it to do, but i dont know how to edit the fragment it creates. I want to set a hint thats different from "search" which is by default. The reason i used this code was because its able to create a sort of dialog or dim the background and creating the the autocompletefragment on top of the first screen which is what i want. Is there an another way to implement the autocomplete?
tl:dr
I want to use the google places autocomplete, and i want it to pop up like a dialog (Dim the first screen and the searchbar goes on top of the first screen).
Does anyone have a solution?
I need to programatically trigger text selection mode in a WebView, but the code I have used does not work on Jelly Bean?
I have been using the following code but it no longer works on Android 4.1 (Jelly Bean) because WebView.selectText, emulateShiftHeld, and the key dispatch are no longer supported on Jelly Bean.
Following code that works on all versions up to ICS is based on: How to enable the default highlight menus in android webview?
public void selectAndCopyText() {
try {
// ICS
WebView.class.getMethod("selectText").invoke(this);
} catch (Exception e1) {
try {
Method m = WebView.class.getMethod("emulateShiftHeld", (Class[])null);
m.invoke(this, (Object[])null);
} catch (Exception e2) {
// fallback
KeyEvent shiftPressEvent = new KeyEvent(0,0,
KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_SHIFT_LEFT,0,0);
shiftPressEvent.dispatch(this);
}
}
}
How do I implement similar functionality that works on Jelly Bean?
I have listed a potential solution in the comments here: How to enable the default highlight menus in android webview?
Here is the content of the potential solution:
After analyzing android.webkit.WebViewClassic I have had some success with the following:
KeyEvent enterEvent = new KeyEvent(0,0,KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_ENTER,0,0);
enterEvent.dispatch(this);
I thought more might be required as I needed to scroll down the WebView a little before the above worked when using an emulator, but after testing on a real JellyBean device the above seems to work fine.
I am writing an android application that takes in a string, read in by a bar code reader, and then when a particular button press occurs, it will send that string to the database and set their values either to "in stock" or "out of stock". I have not been able to find anything on this except for jackcesss, which does not seem to have any really good documentation on it. I cannot even get it to open the file with their example code. The code looks like this:
try {
Database db = Database.open(new File("sdcard/download/Inventory-1.mdb"));
db.close();
} catch (IOException e) {
new AlertDialog.Builder(CheckInActivity.this).setTitle("CRITICAL ERROR").setMessage("DATABASE FILE NOT FOUND. Please check your wireless connection").setPositiveButton("OK",null).show();
e.printStackTrace();
}
When I run this code on my phone I get a force close (I run it on my phone each time to avoid emulator issues,also I am also try to make it work locally before I attempt to make it a remote file) However if I change the location of the file to something I know does not exist, it will catch it and pop up the dialog box that I specified. I tried this with and without closing the file, with throws and the try and catch, nothing seems to work. So what am I missing here?
How do you know the database open was unsuccessful? I'd put in an explicit indication to hit me over the head:
try {
Database db = Database.open(new File("sdcard/download/Inventory-1.mdb"));
new AlertDialog.Builder(CheckInActivity.this).setTitle("SUCCESS").setMessage("DATABASE WAS OPENED SUCCESSFULLY. ").setPositiveButton("OK",null).show(); db.close();
} catch (IOException e) {
new AlertDialog.Builder(CheckInActivity.this).setTitle("CRITICAL ERROR").setMessage("DATABASE FILE NOT FOUND. Please check your wireless connection").setPositiveButton("OK",null).show();
e.printStackTrace();
}
I have some confusion regarding where you put regular java code in an android application.
I'm using the Eclipse SDK and by default when you create an application it makes a .java file with an OnCreate() method. Is this where I would put my code, inside this method?
Right now in my layout I have an imageButton, once this button is clicked I want to open a new WebView page that gets it's HTML code from the index.html file found in the assets folder. This is what I have so far...
Button button = (Button)findViewById(R.id.imagebutton1);
if(button.isPressed())
{
WebView webview = new WebView(null);
setContentView(webview);
try {
InputStream fin = getAssets().open("index.html");
byte[] buffer = new byte[fin.available()];
fin.read(buffer);
fin.close();
webview.loadData(new String(buffer), "text/html", "UTF-8");
} catch (IOException e) {
e.printStackTrace();
}
}
I have this block of code at the end of the onCreate() method right under the line:
setContentView(R.layout.main);
However, once I run the program it crashes and tells me that it failed to start. I'm assuming that it has to do with the fact that the code is in the onCreate. I know that its not where I'm supposed to put it, but I can't think of anywhere else the code should go. Am I supposed to make a new .java file and have a main method there? I'm currently taking classes for C++ and C# so this android thing is still new to me.
Have you done the tutorials? If not I would start there to learn about the basics of creating and working with an Android Activity. Once you have worked your way through them, read the application fundamentals to understand the lifecycle more fully.
You shouldn't be calling setContentView more than once in onCreate. The WebView should be in your main.xml layout file or else launch a new Activity that's layout contains the WebView. And also to hand the onClick on the button you need to call setOnClickListener().
Please see Handling UI Events.
Also, if your program crashes, it would be helpful if you provided error messages from logcat.