搜尋此網誌

2018年3月11日 星期日

【Android】How to increase the height of remoteview in android notification

ref:
https://stackoverflow.com/a/36891181

Notification height is limited to 256dp on Android. Views will be clipped at the bottom that exceed this height.

原來Notification高度是有限制的,不像是bottomsheet可以填充整個畫面。須注意。

【Android】理解RemoteViews

ref:
《Android 开发艺术探索》 05-理解RemoteViews 抄书系列

這篇文章是對解釋remoteview超詳細,比官網還有用,先記錄一下。

如果早先看到這,懂裡面的原理,可以少走冤枉路。

2018年2月25日 星期日

【Java】Regular Expression Pattern


判斷正負實數 



String pattern = "[+-]?((\\d+\\.?\\d*)|(\\.\\d+))";
// Create a Pattern objectPattern r = Pattern.compile(pattern);// Now create matcher object.Matcher m = r.matcher(expression);int x = 0;while (m.find()) {
    Log.d("[FIND]", x + ":-->" + m.group(x++));}

return m.find();



判斷整數加減乘除
規則

expression.replaceAll("^(0+)", "");

或

expression.replaceFirst("^0+", "")

2017年12月31日 星期日

【Android Studio】Instant Run is disabled for non-debug variants


If you have manually set it in the manifest, then remove it and let the IDE automatically assign it. 
If you are using Gradle, make sure that your current variant is debuggable.

buildTypes {
    release {
        debuggable true  //發佈時 記得改為false
    }

2017年11月20日 星期一


https://www.ithome.com.tw/news/118397

除了用戶方面升級外,研究人員也建議,app開發商可以在WindowsManager中啟動FLAG_SECURE參數, 可確保app視窗內容不得被螢幕擷圖,或是在不安全環境下顯示。

2017年11月12日 星期日

【Android】This AsyncTask class should be static or leaks might occur

How to use a static inner AsyncTask class

ref:https://stackoverflow.com/a/46166223

To prevent leaks, you can make the the inner class static. The problem with that, though, is that you no longer have access to the Activity's UI views or member variables. You can pass in a reference to the Context but then you run the same risk of a memory leak. (Android can't garbage collect the Activity after it closes if the AsyncTask class has a strong reference to it.) The solution is to make a weak reference to the Activity (or whatever Context you need).
public class MyActivity extends AppCompatActivity {

    int mSomeMemberVariable = 123;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // start the AsyncTask, passing the Activity context
        // in to a custom constructor 
        new MyTask(this).execute();
    }

    private static class MyTask extends AsyncTask<Void, Void, String> {

        private WeakReference<MyActivity> activityReference;

        // only retain a weak reference to the activity 
        MyTask(MyActivity context) {
            activityReference = new WeakReference<>(context);
        }

        @Override
        protected String doInBackground(Void... params) {

            // do some long running task...

            return "task finished";
        }

        @Override
        protected void onPostExecute(String result) {

            // get a reference to the activity if it is still there
            MyActivity activity = activityReference.get();
            if (activity == null) return;

            // modify the activity's UI
            TextView textView = activity.findViewById(R.id.textview);
            textView.setText(result);

            // access Activity member variables
            activity.mSomeMemberVariable = 321;
        }
    }
}

Notes

  • As far as I know, this type of memory leak danger has always been true, but I only started seeing the warning in Android Studio 3.0. A lot of the main AsyncTask tutorials out there still don't deal with it (see here, here, here, and here).
  • You would also follow a similar procedure if your AsyncTask were a top-level class. A static inner class is basically the same as a top-level class in Java.
  • If you don't need the Activity itself but still want the Context (for example, to display a Toast), you can pass in a reference to the app context. In this case the AsyncTask constructor would look like this:
    private WeakReference<Application> appReference;
    
    MyTask(Application context) {
        appReference = new WeakReference<>(context);
    }
  • There are some arguments out there for ignoring this warning and just using the non-static class. After all, the AsyncTask is intended to be very short lived (a couple seconds at the longest), and it will release its reference to the Activity when it finishes anyway. See this and this.
  • Excellent article: How to Leak a Context: Handlers & Inner Classes