2016年3月7日 星期一

Send android broadcast by adb

Reference:

http://blog.csdn.net/yelangjueqi/article/details/43231425

Command:

adb shell am broadcast:

[-a <ACTION>]
[-d <DATA_URI>]
[-t <MIME_TYPE>]
[-c <CATEGORY> [-c <CATEGORY>] ...]
[-e|--es <EXTRA_KEY> <EXTRA_STRING_VALUE> ...]
[--ez <EXTRA_KEY> <EXTRA_BOOLEAN_VALUE> ...]
[-e|--ei <EXTRA_KEY> <EXTRA_INT_VALUE> ...]
[-n <COMPONENT>]
[-f <FLAGS>] [<URI>]

Example:
adb shell am broadcast -a com.android.test --es test_string "this is test string" --ei test_int 100 --ez test_boolean true

2016年3月6日 星期日

How to animate a TextView using TextSwitcher

Reference:
http://www.learn-android-easily.com/2013/06/android-textswitcher.html
http://www.cnblogs.com/over140/archive/2010/10/22/1857991.html
http://never-say-never.iteye.com/blog/976886


Code snippet:

public class MainActivity implements ViewSwitcher.ViewFactory{
    private TextSwitcher mTextSwitcher;
    private Button mButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ........
        mTextSwitcher = (TextSwitcher) findViewById(R.id.text_switcher);
        mTextSwitcher.setFactory(this);

       // Declare the in and out animations and initialize them
       Animation in = AnimationUtils.loadAnimation(this,android.R.anim.slide_in_left);
       Animation out = AnimationUtils.loadAnimation(this,android.R.anim.slide_out_right);
                     
       // set the animation type of textSwitcher
       mSwitcher.setInAnimation(in);
       mSwitcher.setOutAnimation(out);

        mButton = (Button) findViewById(R.id.button);
        mButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                mSwitcher.setText(myTextString);
            }
        });
    }

    // 重寫 ViewSwitcher.ViewFactory 的 makeView()方法,返回一個 View,TextSwitcher 交換時 使用  
    @Override 
    public View makeView() {  
        TextView textView = new TextView(this);  
        textView.setTextSize(36);
        textView.setColor(Color.White);
        textView.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
        return textView;  
    }
}

IllegalArgumentException: View not attached to window manager

Reference:

http://stackoverflow.com/questions/22924825/view-not-attached-to-window-manager-crash
http://www.2cto.com/kf/201202/119310.html

Scenario:

UI thread產生另一個 thread(Ex: AsyncTask)來顯示一個 Dialog,當 thread任務完成了再 dismiss dialog,如果在此期間 Activity因為某種原因被殺掉且又重新啟動了,那麼當  Dismiss的時候, WindowManager檢查發現  Dialog所屬的  Activity已經不存在了,就會跳出這個 Exception

Code snippet:

@Override
protected void onDestroy() {
    dismissProgressDialog();
    super.onDestroy();
}

protected void onPostExecute(String file_url)
{
    if (YourActivity.this.isDestroyed()) { // or call isFinishing() if min sdk version < 17
        return;
    }
    dismissProgressDialog();
    something(note);
}

private void dismissProgressDialog() {
    if (mDialog != null && mDialog.isShowing()) {
        mDialog.dismiss();
    }
}


2016年1月25日 星期一

Close Google Map toolbar

Reference:
https://developers.google.com/maps/documentation/android-api/controls#toolbar

Code sinppet:
mMap.getUiSettings().setMapToolbarEnabled(false);

2016年1月11日 星期一

[Exception] couldn't find class 'com.google.android.gms.measurement.internal.zzz'

References:

http://stackoverflow.com/questions/33196015/error-on-some-devices-couldnt-find-class-com-google-android-gms-measurement

Error log:

01-12 14:45:58.910    4347-4347/? E/dalvikvm﹕ Could not find class 'com.google.android.gms.measurement.internal.zzx', referenced from method com.google.android.gms.measurement.internal.zzt.zzaU

    java.lang.VerifyError: com/google/android/gms/measurement/internal/zzt
            at com.google.android.gms.measurement.AppMeasurementContentProvider.onCreate(Unknown Source)
            at android.content.ContentProvider.attachInfo(ContentProvider.java:1591)
            at android.content.ContentProvider.attachInfo(ContentProvider.java:1562)
            at android.app.ActivityThread.installProvider(ActivityThread.java:4897)
            at android.app.ActivityThread.installContentProviders(ActivityThread.java:4487)
            at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4427)
            at android.app.ActivityThread.access$1500(ActivityThread.java:143)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1302)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5124)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:613)
            at dalvik.system.NativeStart.main(Native Method)

Code snippet:

MyApplication.java
public class MyApplication extends Application {

    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
}

AndroidManifest.xml
<application
        android:name=".MyApplication"
        android:allowBackup="true"
        android:icon="@mipmap/xxxxxx"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

Solution:

This happens because of multi-dexing. When your code is so big that it can't be compiled with a single dex file, we use multidexing. But after that it wouldn't run on all phones

2016年1月10日 星期日

Get application directory

Reference:

http://stackoverflow.com/questions/5527764/get-application-directory

Code snippet:

PackageManager m = getPackageManager();
String s = getPackageName();
try {
    PackageInfo p = m.getPackageInfo(s, 0);
    s = p.applicationInfo.dataDir;
} catch (PackageManager.NameNotFoundException e) {
    Log.w("yourtag", "Error Package name not found ", e);
}

2016年1月8日 星期五

Open Google Play Store for your application

Reference:

http://stackoverflow.com/questions/11753000/how-to-open-the-google-play-store-directly-from-my-android-application

Code snippet:

String appPackageName = "Your app package name";
try {
    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
} catch (android.content.ActivityNotFoundException anfe) {
    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
}