In App Advertisements

Google has AdSense and AdWords

There is MoPub that people use and talk about. Only experience can tell (or do You?) what is better.

I had code sample for MoPub, let’s try it out.

1. MoPub Integration part1

http://help.mopub.com/customer/portal/articles/285186

This crashes at runtime…

Though I used MoPub successfully earlier (in sprint). Spent 2-3 and then switched to AdMob.

I have low tolerance to now this works now it doesn’t game.

2. AdMob by google. Easy to integrate, seem to be working well so far.

 

 

Advertisements

Send mail from the app

We want to implement Contact Us button. 

User will be able to send an e-mail to my designated mailbox. [I might check it :)]

1. 

Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"recipient@example.com"});
i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
i.putExtra(Intent.EXTRA_TEXT   , "body of email");
try {
    startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
    Toast.makeText(MyActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
} 

But this requires e-mail client (?) and doesnt have sender address. How would I respond then?

2.
Intent intent = new Intent(Intent.ACTION_SENDTO); // it's not ACTION_SEND intent.setType("text/plain"); intent.putExtra(Intent.EXTRA_SUBJECT, "Subject of email"); intent.putExtra(Intent.EXTRA_TEXT, "Body of email"); intent.setData(Uri.parse("mailto:default@recipient.com")); // or just "mailto:" for blank intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // this will make such that when user returns to your app, your app is displayed, instead of the email app. startActivity(intent);
3. http://stackoverflow.com/questions/2197741/how-to-send-email-from-my-android-application
more code examples.
One of these should work I hope.

Preferences/Settings

Preferences (Settings) are used for storing values between sessions. These values could be save in a database but when values are just a few it can ge in preferences

1. Create

private void saveMyPreferences(String key, String value) {

SharedPreferences preferences = getApplicationContext().getSharedPreferences(A_PREFERENCES_NAME, 0);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(key, value);
editor.commit();
return ;
}

2. retrieve
private String getMyPreferences(String key) {
SharedPreferences preferences = getPreferences(A_PREFERENCES_NAME);

String defaultValue=””;

English: An Inglehart-Welzel Cultural Map of t...

English: An Inglehart-Welzel Cultural Map of the World: World Secular-Rational and Self Expression Values as a map of world cultures based on World Values Survey data. Svenska: En Världskulturkarta av Inglehart-Welzel typ. (Photo credit: Wikipedia)

return preferences.getString(key,defaultValue);
}

Unhandled Exceptions

After my app crashes on device it become unresponsive.  I have to go to setup and force stop.

No one would do this.. Let’s see if there is a way to kill the app and restart. Would be great to catch a crash log and e-mail it or copy to google docs. Will look into that later. For now I want the app to be always responsive.

Thread.UncaughtExceptionHandler onRuntimeError=
new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread,
Throwable ex) {
Log.d(TAG, “uncaughtException!!!!!!!”);
/* ex.printStackTrace();
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);*/
// here I do logging of exception to a db
PendingIntent myActivity = PendingIntent.getActivity(getBaseContext(),
192837, new Intent(getBaseContext(), TilesActivity.class),
PendingIntent.FLAG_ONE_SHOT);
// PendingIntent myActivity = PendingIntent.getActivity(this.getInstance().getBaseContext(), 0,
// new Intent(getIntent()), getIntent().getFlags());
AlarmManager alarmManager;
alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
15000, myActivity );
System.exit(2);

// re-throw critical exception further to the os (important)
defaultUEH.uncaughtException(thread, ex);

}};
Thread.setDefaultUncaughtExceptionHandler(onRuntimeError);

Works well. I put intentional divide by zero deep into my code. App got restarted. Some things are easy.

Now. How to e-mail the log?

Let’s collect data

1. https://code.google.com/p/android-log-collector/

2. http://acra.ch/

3. https://www.bugsense.com/ this claims to include real-time intelligence

4.  integrate Google Analytics into your app? You can configure to send crash reports every time your app freezes with stacktrace and all.

5. Ah, this is promising https://developers.google.com/analytics/devguides/collection/android/

 

 

Hide soft keyboard

I have two fields on my activity, say : name and description.

When entering name the keyboard shows “Next”. After I go next to a description there is nothing to remove the keyboard. OK,  my motorolla phone has arrow down to remove the keyboard. My other device doesn’t have anything to kill the keyboard. Very frustrating. Let’s do this programmatically.

Code to hide the keyboard:

public void hideSoftKeyboard(Activity activity) {
InputMethodManager inputMethodManager = (InputMethodManager)                  activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}

Method to invoke this code
public void setupUI(View view, Activity act) {
final Activity mAct= act;
//Set up touch listener for non-text box views to hide keyboard.
if(!(view instanceof EditText)) {

view.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
hideSoftKeyboard(mAct);
return false;
}

});
}

//If a layout container, iterate over children and seed recursion.
if (view instanceof ViewGroup) {

for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {

View innerView = ((ViewGroup) view).getChildAt(i);

setupUI(innerView, mAct);
}
}
}

 

In the activity put one liner:

new Keyboard().setupUI(findViewById(R.id.container), MyActivity.this);

 

Works! Yey!