├── .gitignore ├── .idea ├── .name ├── codeStyleSettings.xml ├── compiler.xml ├── copyright │ ├── monkey.xml │ └── profiles_settings.xml ├── dictionaries │ └── monkey.xml ├── encodings.xml ├── gradle.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml └── vcs.xml ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── sunjiajia │ │ └── monkeyandroid │ │ └── ApplicationTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── sunjiajia │ │ │ └── monkeyandroid │ │ │ ├── MyApplication.java │ │ │ ├── baselearn │ │ │ └── activity │ │ │ │ ├── BaseActivity.java │ │ │ │ ├── ButtonActivity.java │ │ │ │ ├── CheckBoxRadioButtonActivity.java │ │ │ │ ├── FrameGridLayoutActivity.java │ │ │ │ ├── ImageViewActivity.java │ │ │ │ ├── LinearLayoutActivity.java │ │ │ │ ├── MainActivity.java │ │ │ │ ├── RelativeTableLayoutActivity.java │ │ │ │ ├── SpinnerAutoCompleteTextViewActivity.java │ │ │ │ ├── TextViewEditTextActivity.java │ │ │ │ ├── ToastActivity.java │ │ │ │ └── ToggleButtonRadioBarActivity.java │ │ │ ├── customwidgets │ │ │ ├── activity │ │ │ │ ├── CustomWidgetsActivity.java │ │ │ │ └── ShowWidgetsActivity.java │ │ │ ├── fragment │ │ │ │ └── WidgetFragment.java │ │ │ └── widgets │ │ │ │ └── RatioImageView.java │ │ │ ├── domain │ │ │ └── Message.java │ │ │ ├── mustlearn │ │ │ ├── MustLearnPointActivity.java │ │ │ ├── activitydata │ │ │ │ ├── FirstActivity.java │ │ │ │ └── TwoActivity.java │ │ │ ├── broadcastreceiverservicedemo │ │ │ │ ├── BcrActivity.java │ │ │ │ ├── IpCallReceiver.java │ │ │ │ ├── MyService.java │ │ │ │ └── SmsReceiver.java │ │ │ ├── dagger2 │ │ │ │ ├── Dagger2Activity.java │ │ │ │ ├── adapter │ │ │ │ │ └── UserAdapter.java │ │ │ │ └── injection │ │ │ │ │ ├── ActivityScope.java │ │ │ │ │ ├── component │ │ │ │ │ └── UserComponent.java │ │ │ │ │ └── modules │ │ │ │ │ └── UserModule.java │ │ │ ├── databinding │ │ │ │ ├── CapitalUtil.java │ │ │ │ ├── DataBindingActivity.java │ │ │ │ ├── adapter │ │ │ │ │ └── RecyclerAdapter.java │ │ │ │ └── bean │ │ │ │ │ └── User.java │ │ │ ├── recyclerview │ │ │ │ ├── BaseRecyclerAdapter.java │ │ │ │ ├── BaseRecyclerHolder.java │ │ │ │ ├── MyRecyclerAdapter.java │ │ │ │ ├── MyRecyclerHolder.java │ │ │ │ └── SuperRecyclerActivity.java │ │ │ └── sqlite │ │ │ │ ├── MySQLiteOpenHelper.java │ │ │ │ ├── SqliteActivity.java │ │ │ │ └── SqliteListViewAdapter.java │ │ │ ├── test │ │ │ └── UnitTestDemo.java │ │ │ └── utils │ │ │ ├── PermissionsUtil.java │ │ │ └── Utils.java │ └── res │ │ ├── drawable │ │ ├── bg_btn.xml │ │ └── monkey.jpg │ │ ├── layout │ │ ├── activity_bcr.xml │ │ ├── activity_btn.xml │ │ ├── activity_checkbox_radiobutton.xml │ │ ├── activity_custom_widgets.xml │ │ ├── activity_dagger2.xml │ │ ├── activity_data_binding.xml │ │ ├── activity_first.xml │ │ ├── activity_frame_grid_layout.xml │ │ ├── activity_imageview.xml │ │ ├── activity_linear_layout.xml │ │ ├── activity_main.xml │ │ ├── activity_must_learn_point.xml │ │ ├── activity_relative_table_layout.xml │ │ ├── activity_show_widgets.xml │ │ ├── activity_spinner_autocompletetextview.xml │ │ ├── activity_sqlite.xml │ │ ├── activity_super_recycler.xml │ │ ├── activity_textview_edittext.xml │ │ ├── activity_toast.xml │ │ ├── activity_togglebutton_radiobar.xml │ │ ├── activity_two.xml │ │ ├── frag_ratio_imageview.xml │ │ ├── item_recycler.xml │ │ ├── item_recycler_binding.xml │ │ ├── item_sqlite.xml │ │ └── item_user.xml │ │ ├── menu │ │ └── menu_main.xml │ │ ├── mipmap-hdpi │ │ ├── author.png │ │ ├── ic_explore_white_24dp.png │ │ ├── ic_launcher.png │ │ ├── ic_widgets_white_24dp.png │ │ └── super_dog.jpg │ │ ├── mipmap-mdpi │ │ ├── ic_explore_white_24dp.png │ │ ├── ic_launcher.png │ │ └── ic_widgets_white_24dp.png │ │ ├── mipmap-xhdpi │ │ ├── ic_explore_white_24dp.png │ │ ├── ic_launcher.png │ │ └── ic_widgets_white_24dp.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_explore_white_24dp.png │ │ ├── ic_launcher.png │ │ └── ic_widgets_white_24dp.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_explore_white_24dp.png │ │ ├── ic_launcher.png │ │ └── ic_widgets_white_24dp.png │ │ ├── values-w820dp │ │ └── dimens.xml │ │ └── values │ │ ├── arrays.xml │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── sunjiajia │ └── monkeyandroid │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | -------------------------------------------------------------------------------- /.idea/.name: -------------------------------------------------------------------------------- 1 | MonkeyAndroid -------------------------------------------------------------------------------- /.idea/codeStyleSettings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 227 | 229 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /.idea/copyright/monkey.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /.idea/dictionaries/monkey.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 23 | 24 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 19 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 46 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## 简介 2 | 3 | [博客](http://sunjiajia.com)上的Android基础课程《Monkey Android》中所使用的例子。供大家在学习的过程中参考。 4 | 5 | ## 效果(目前已完成) 6 | 7 | ### 基础 8 | 9 | * LinearLayout布局与点击事件的4种写法 10 | * RelativeLayout和TableLayout的用法 11 | * FrameLayout和GridLayout的用法 12 | * Toast的用法 13 | * TextView和EditText的用法 14 | * Button和ImageButton的用法 15 | * ImageView的用法 16 | * CheckBox和RadioButton的用法 17 | * ToggleButton和Radiobar的用法 18 | * Spinner和AutoCompleteTextView的用法 19 | 20 | 21 | ### 必备知识点 22 | 23 | * 知识点MustLearnPointActivity这个Activity只用了Butterknife 24 | * MustLearnPointActivity中使用了权限PermissionsUtil工具类,适配API 23上的权限控制 25 | * 获取SD卡剩余空间 26 | * 使用XML备份短信 27 | * 解析XML 28 | * 精通 Android Data Binding的用法及示例 29 | 30 | ### 自定义控件 31 | 32 | * 写了一个简单的按比例显示图片的ImageView,可调用setRatio方法设置宽高比显示图片 33 | 34 | ## 微信公众号:Android奇想录(android_amazing) 35 | 36 | ![Android奇想录](http://sunjiajia.com/img/qrcode_for_android_amazing.jpg) -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * 4 | * * * =================================== 5 | * * * Copyright (c) 2016. 6 | * * * 作者:安卓猴 7 | * * * 微博:@安卓猴 8 | * * * 博客:http://sunjiajia.com 9 | * * * Github:https://github.com/opengit 10 | * * * 11 | * * * 注意**:如果您使用或者修改该代码,请务必保留此版权信息。 12 | * * * =================================== 13 | * * 14 | * * 15 | * 16 | */ 17 | 18 | apply plugin: 'com.android.application' 19 | // android-apt 20 | apply plugin: 'com.neenbedankt.android-apt' 21 | 22 | android { 23 | signingConfigs { 24 | app { 25 | keyAlias 'sunjiajia' 26 | keyPassword '9985979aa' 27 | storeFile file('/home/monkey/Projects/AndroidStudioProjects/sunjiajia.jks') 28 | storePassword '9985979aa' 29 | } 30 | } 31 | compileSdkVersion 24 32 | buildToolsVersion '23.0.3' 33 | 34 | defaultConfig { 35 | applicationId "com.sunjiajia.monkeyandroid" 36 | minSdkVersion 14 37 | targetSdkVersion 24 38 | versionCode 1 39 | versionName "1.0" 40 | signingConfig signingConfigs.app 41 | } 42 | 43 | dataBinding { 44 | enabled true 45 | } 46 | 47 | buildTypes { 48 | release { 49 | minifyEnabled false 50 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 51 | signingConfig signingConfigs.app 52 | zipAlignEnabled true 53 | } 54 | debug { 55 | signingConfig signingConfigs.app 56 | } 57 | } 58 | 59 | testOptions { 60 | unitTests.returnDefaultValues = true 61 | } 62 | 63 | productFlavors {} 64 | } 65 | 66 | dependencies { 67 | compile fileTree(include: ['*.jar'], dir: 'libs') 68 | testCompile 'junit:junit:4.12' 69 | compile 'com.jakewharton:butterknife:7.0.1' 70 | compile 'com.android.support:design:24.0.0' 71 | compile 'com.android.support:appcompat-v7:24.0.0' 72 | // dagger2 73 | compile 'com.google.dagger:dagger:2.2' 74 | // dagger2 75 | apt 'com.google.dagger:dagger-compiler:2.2' 76 | provided 'javax.annotation:javax.annotation-api:1.2' 77 | } 78 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /home/monkey/Android/android-sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/sunjiajia/monkeyandroid/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * 4 | * * * =================================== 5 | * * * Copyright (c) 2016. 6 | * * * 作者:安卓猴 7 | * * * 微博:@安卓猴 8 | * * * 博客:http://sunjiajia.com 9 | * * * Github:https://github.com/opengit 10 | * * * 11 | * * * 注意**:如果您使用或者修改该代码,请务必保留此版权信息。 12 | * * * =================================== 13 | * * 14 | * * 15 | * 16 | */ 17 | 18 | package com.sunjiajia.monkeyandroid; 19 | 20 | import android.app.Application; 21 | import android.test.ApplicationTestCase; 22 | 23 | /** 24 | * Testing Fundamentals 25 | */ 26 | public class ApplicationTest extends ApplicationTestCase { 27 | public ApplicationTest() { 28 | super(Application.class); 29 | } 30 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 18 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | // 打电话广播接收者需要的权限 32 | 33 | 34 | 35 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | // 广播接受者必须在清单文件中配置 68 | 69 | 70 | //这里指定广播接收者接收哪个广播,这里时打电话广播 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | -------------------------------------------------------------------------------- /app/src/main/java/com/sunjiajia/monkeyandroid/MyApplication.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * 4 | * * * =================================== 5 | * * * Copyright (c) 2016. 6 | * * * 作者:安卓猴 7 | * * * 微博:@安卓猴 8 | * * * 博客:http://sunjiajia.com 9 | * * * Github:https://github.com/opengit 10 | * * * 11 | * * * 注意**:如果您使用或者修改该代码,请务必保留此版权信息。 12 | * * * =================================== 13 | * * 14 | * * 15 | * 16 | */ 17 | 18 | package com.sunjiajia.monkeyandroid; 19 | 20 | import android.app.Application; 21 | 22 | /** 23 | * Created by Monkey on 2016/4/16. 24 | */ 25 | public class MyApplication extends Application { 26 | 27 | @Override public void onCreate() { 28 | super.onCreate(); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /app/src/main/java/com/sunjiajia/monkeyandroid/baselearn/activity/BaseActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * 4 | * * * =================================== 5 | * * * Copyright (c) 2016. 6 | * * * 作者:安卓猴 7 | * * * 微博:@安卓猴 8 | * * * 博客:http://sunjiajia.com 9 | * * * Github:https://github.com/opengit 10 | * * * 11 | * * * 注意**:如果您使用或者修改该代码,请务必保留此版权信息。 12 | * * * =================================== 13 | * * 14 | * * 15 | * 16 | */ 17 | 18 | package com.sunjiajia.monkeyandroid.baselearn.activity; 19 | 20 | import android.os.Bundle; 21 | import android.support.annotation.Nullable; 22 | import android.support.v7.app.AppCompatActivity; 23 | 24 | /** 25 | * Created by monkey on 12/6/15. 26 | */ 27 | public abstract class BaseActivity extends AppCompatActivity { 28 | 29 | public abstract int giveViewResId(); 30 | 31 | @Override protected void onCreate(@Nullable Bundle savedInstanceState) { 32 | super.onCreate(savedInstanceState); 33 | setContentView(giveViewResId()); 34 | setActivityTitle(); 35 | } 36 | 37 | private void setActivityTitle() { 38 | setTitle(getIntent().getStringExtra("title")); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /app/src/main/java/com/sunjiajia/monkeyandroid/baselearn/activity/ButtonActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * 4 | * * * =================================== 5 | * * * Copyright (c) 2016. 6 | * * * 作者:安卓猴 7 | * * * 微博:@安卓猴 8 | * * * 博客:http://sunjiajia.com 9 | * * * Github:https://github.com/opengit 10 | * * * 11 | * * * 注意**:如果您使用或者修改该代码,请务必保留此版权信息。 12 | * * * =================================== 13 | * * 14 | * * 15 | * 16 | */ 17 | 18 | package com.sunjiajia.monkeyandroid.baselearn.activity; 19 | 20 | import android.os.Bundle; 21 | import android.support.annotation.Nullable; 22 | import android.view.View; 23 | import android.widget.Toast; 24 | import com.sunjiajia.monkeyandroid.R; 25 | 26 | /** 27 | * Created by monkey on 12/9/15. 28 | */ 29 | public class ButtonActivity extends BaseActivity { 30 | 31 | @Override public int giveViewResId() { 32 | return R.layout.activity_btn; 33 | } 34 | 35 | @Override protected void onCreate(@Nullable Bundle savedInstanceState) { 36 | super.onCreate(savedInstanceState); 37 | } 38 | 39 | /** 40 | * Button的点击事件 41 | */ 42 | public void btnClicked(View view) { 43 | switch (view.getId()) { 44 | case R.id.btn_btn: 45 | Toast.makeText(ButtonActivity.this, "我是安卓猴", Toast.LENGTH_SHORT).show(); 46 | break; 47 | case R.id.btn_transparent_btn: 48 | Toast.makeText(ButtonActivity.this, "透明背景Button", Toast.LENGTH_SHORT).show(); 49 | break; 50 | case R.id.btn_selector_btn: 51 | Toast.makeText(ButtonActivity.this, "设置selector的Button", Toast.LENGTH_SHORT).show(); 52 | break; 53 | case R.id.imagebtn: 54 | Toast.makeText(ButtonActivity.this, "我是ImageButton", Toast.LENGTH_SHORT).show(); 55 | break; 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /app/src/main/java/com/sunjiajia/monkeyandroid/baselearn/activity/CheckBoxRadioButtonActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * 4 | * * * =================================== 5 | * * * Copyright (c) 2016. 6 | * * * 作者:安卓猴 7 | * * * 微博:@安卓猴 8 | * * * 博客:http://sunjiajia.com 9 | * * * Github:https://github.com/opengit 10 | * * * 11 | * * * 注意**:如果您使用或者修改该代码,请务必保留此版权信息。 12 | * * * =================================== 13 | * * 14 | * * 15 | * 16 | */ 17 | 18 | package com.sunjiajia.monkeyandroid.baselearn.activity; 19 | 20 | import android.os.Bundle; 21 | import android.support.annotation.Nullable; 22 | import android.widget.CheckBox; 23 | import android.widget.CompoundButton; 24 | import android.widget.RadioButton; 25 | import android.widget.RadioGroup; 26 | import android.widget.Toast; 27 | import com.sunjiajia.monkeyandroid.R; 28 | 29 | /** 30 | * Created by monkey on 1/1/16. 31 | * 32 | * 实现了CheckButton的接口CompoundButton.OnCheckedChangeListener 33 | * 34 | * 实现了RadioGroup的接口RadioGroup.OnCheckedChangeListener 35 | */ 36 | public class CheckBoxRadioButtonActivity extends BaseActivity 37 | implements CompoundButton.OnCheckedChangeListener, RadioGroup.OnCheckedChangeListener { 38 | private CheckBox mCbXs, mCbYx, mCbDy; 39 | private RadioGroup mRg; 40 | private RadioButton mRbBanana, mRbApple, mRbOrange; 41 | 42 | @Override public int giveViewResId() { 43 | return R.layout.activity_checkbox_radiobutton; 44 | } 45 | 46 | @Override protected void onCreate(@Nullable Bundle savedInstanceState) { 47 | super.onCreate(savedInstanceState); 48 | 49 | initViews(); 50 | 51 | configViews(); 52 | } 53 | 54 | /** 55 | * 配置各种控件,在这里只是设置了绑定了监听事件 56 | */ 57 | private void configViews() { 58 | mCbXs.setOnCheckedChangeListener(this); 59 | mCbYx.setOnCheckedChangeListener(this); 60 | mCbDy.setOnCheckedChangeListener(this); 61 | 62 | mRg.setOnCheckedChangeListener(this); 63 | } 64 | 65 | /** 66 | * init各种控件 67 | */ 68 | private void initViews() { 69 | mCbXs = (CheckBox) findViewById(R.id.checkbox_xiaoshuo); 70 | mCbYx = (CheckBox) findViewById(R.id.checkbox_youxi); 71 | mCbDy = (CheckBox) findViewById(R.id.checkbox_dianying); 72 | 73 | mRg = (RadioGroup) findViewById(R.id.radio_group); 74 | 75 | mRbBanana = (RadioButton) findViewById(R.id.radiobutton_banana); 76 | mRbApple = (RadioButton) findViewById(R.id.radiobutton_apple); 77 | mRbOrange = (RadioButton) findViewById(R.id.radiobutton_orange); 78 | } 79 | 80 | /** 81 | * CheckBox的监听事件 82 | */ 83 | @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 84 | 85 | switch (buttonView.getId()) { 86 | case R.id.checkbox_xiaoshuo: 87 | checkedShowToast(buttonView, isChecked); 88 | break; 89 | case R.id.checkbox_youxi: 90 | checkedShowToast(buttonView, isChecked); 91 | break; 92 | case R.id.checkbox_dianying: 93 | checkedShowToast(buttonView, isChecked); 94 | break; 95 | } 96 | } 97 | 98 | /** 99 | * RadioGroup的监听事件 100 | */ 101 | @Override public void onCheckedChanged(RadioGroup group, int checkedId) { 102 | 103 | switch (checkedId) { 104 | case R.id.radiobutton_banana: 105 | radioGroupChecked(mRbBanana); 106 | break; 107 | case R.id.radiobutton_apple: 108 | radioGroupChecked(mRbApple); 109 | break; 110 | case R.id.radiobutton_orange: 111 | radioGroupChecked(mRbOrange); 112 | break; 113 | } 114 | } 115 | 116 | private void radioGroupChecked(RadioButton button) { 117 | Toast.makeText(CheckBoxRadioButtonActivity.this, "您选中了#" + button.getText().toString() + "#", 118 | Toast.LENGTH_SHORT).show(); 119 | } 120 | 121 | private void checkedShowToast(CompoundButton buttonView, boolean isChecked) { 122 | if (isChecked) { 123 | Toast.makeText(CheckBoxRadioButtonActivity.this, 124 | "您选中了#" + buttonView.getText().toString() + "#", Toast.LENGTH_SHORT).show(); 125 | } else { 126 | Toast.makeText(CheckBoxRadioButtonActivity.this, 127 | "您取消了#" + buttonView.getText().toString() + "#", Toast.LENGTH_SHORT).show(); 128 | } 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /app/src/main/java/com/sunjiajia/monkeyandroid/baselearn/activity/FrameGridLayoutActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * 4 | * * * =================================== 5 | * * * Copyright (c) 2016. 6 | * * * 作者:安卓猴 7 | * * * 微博:@安卓猴 8 | * * * 博客:http://sunjiajia.com 9 | * * * Github:https://github.com/opengit 10 | * * * 11 | * * * 注意**:如果您使用或者修改该代码,请务必保留此版权信息。 12 | * * * =================================== 13 | * * 14 | * * 15 | * 16 | */ 17 | 18 | package com.sunjiajia.monkeyandroid.baselearn.activity; 19 | 20 | import android.os.Bundle; 21 | import android.support.annotation.Nullable; 22 | import com.sunjiajia.monkeyandroid.R; 23 | 24 | /** 25 | * Created by monkey on 12/6/15. 26 | */ 27 | public class FrameGridLayoutActivity extends BaseActivity { 28 | @Override public int giveViewResId() { 29 | return R.layout.activity_frame_grid_layout; 30 | } 31 | 32 | @Override protected void onCreate(@Nullable Bundle savedInstanceState) { 33 | super.onCreate(savedInstanceState); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /app/src/main/java/com/sunjiajia/monkeyandroid/baselearn/activity/ImageViewActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * 4 | * * * =================================== 5 | * * * Copyright (c) 2016. 6 | * * * 作者:安卓猴 7 | * * * 微博:@安卓猴 8 | * * * 博客:http://sunjiajia.com 9 | * * * Github:https://github.com/opengit 10 | * * * 11 | * * * 注意**:如果您使用或者修改该代码,请务必保留此版权信息。 12 | * * * =================================== 13 | * * 14 | * * 15 | * 16 | */ 17 | 18 | package com.sunjiajia.monkeyandroid.baselearn.activity; 19 | 20 | import android.os.Bundle; 21 | import android.support.annotation.Nullable; 22 | import android.view.View; 23 | import android.widget.Toast; 24 | import com.sunjiajia.monkeyandroid.R; 25 | 26 | /** 27 | * Created by monkey on 12/18/15. 28 | */ 29 | public class ImageViewActivity extends BaseActivity { 30 | 31 | @Override public int giveViewResId() { 32 | return R.layout.activity_imageview; 33 | } 34 | 35 | @Override protected void onCreate(@Nullable Bundle savedInstanceState) { 36 | super.onCreate(savedInstanceState); 37 | } 38 | 39 | public void ivClicked(View view) { 40 | switch (view.getId()) { 41 | case R.id.imageview_normal: 42 | Toast.makeText(ImageViewActivity.this, "我是普通ImageView,我没有保持宽高比,我有黑边。", Toast.LENGTH_SHORT) 43 | .show(); 44 | break; 45 | case R.id.imageview_super: 46 | Toast.makeText(ImageViewActivity.this, "我是超级ImageView,我保持了宽高比,我没有黑边。", Toast.LENGTH_SHORT) 47 | .show(); 48 | break; 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /app/src/main/java/com/sunjiajia/monkeyandroid/baselearn/activity/LinearLayoutActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * 4 | * * * =================================== 5 | * * * Copyright (c) 2016. 6 | * * * 作者:安卓猴 7 | * * * 微博:@安卓猴 8 | * * * 博客:http://sunjiajia.com 9 | * * * Github:https://github.com/opengit 10 | * * * 11 | * * * 注意**:如果您使用或者修改该代码,请务必保留此版权信息。 12 | * * * =================================== 13 | * * 14 | * * 15 | * 16 | */ 17 | 18 | package com.sunjiajia.monkeyandroid.baselearn.activity; 19 | 20 | import android.os.Bundle; 21 | import android.view.View; 22 | import android.widget.Button; 23 | import android.widget.Toast; 24 | import com.sunjiajia.monkeyandroid.R; 25 | 26 | /** 27 | * 《Monkey Android》第6课点击事件的四种写法 28 | * http://sunjiajia.com/2015/08/10/monkey-android-6/ 29 | */ 30 | public class LinearLayoutActivity extends BaseActivity implements View.OnClickListener { 31 | 32 | private Button mButton01; 33 | private Button mButton02; 34 | private Button mButton03; 35 | private Button mButton04; 36 | 37 | @Override public int giveViewResId() { 38 | return R.layout.activity_linear_layout; 39 | } 40 | 41 | @Override protected void onCreate(Bundle savedInstanceState) { 42 | super.onCreate(savedInstanceState); 43 | 44 | mButton01 = (Button) findViewById(R.id.btn01); 45 | mButton02 = (Button) findViewById(R.id.btn02); 46 | mButton03 = (Button) findViewById(R.id.btn03); 47 | mButton04 = (Button) findViewById(R.id.btn04); 48 | 49 | // 第1种写法: 50 | mButton01.setOnClickListener(new View.OnClickListener() { 51 | @Override public void onClick(View v) { 52 | Toast.makeText(LinearLayoutActivity.this, mButton01.getClass().toString() + "--> Button01", 53 | Toast.LENGTH_SHORT).show(); 54 | } 55 | }); 56 | 57 | // 第2种写法:主类实现OnClickListener接口,并在主类中复写方法onClick 58 | mButton02.setOnClickListener(this); 59 | 60 | // 第3种写法:内部类实现OnClickListener接口,复写方法onClick 61 | MyButtonClickListener mButtonClickListener = new MyButtonClickListener(); 62 | mButton03.setOnClickListener(mButtonClickListener); 63 | } 64 | 65 | // 第4种写法:在布局文件中给Button04添加android:onClick="clickButton04"属性,并指定了点击触发的方法clickButton04 66 | public void clickButton04(View view) { 67 | 68 | Toast.makeText(this, mButton04.getClass().toString() + "--> Button04", Toast.LENGTH_SHORT) 69 | .show(); 70 | } 71 | 72 | @Override public void onClick(View v) { 73 | 74 | Toast.makeText(LinearLayoutActivity.this, mButton02.getClass().toString() + "--> Button02", 75 | Toast.LENGTH_SHORT).show(); 76 | } 77 | 78 | class MyButtonClickListener implements View.OnClickListener { 79 | 80 | @Override public void onClick(View v) { 81 | Toast.makeText(LinearLayoutActivity.this, mButton03.getClass().toString() + "--> Button03", 82 | Toast.LENGTH_SHORT).show(); 83 | } 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /app/src/main/java/com/sunjiajia/monkeyandroid/baselearn/activity/MainActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * 4 | * * * =================================== 5 | * * * Copyright (c) 2016. 6 | * * * 作者:安卓猴 7 | * * * 微博:@安卓猴 8 | * * * 博客:http://sunjiajia.com 9 | * * * Github:https://github.com/opengit 10 | * * * 11 | * * * 注意**:如果您使用或者修改该代码,请务必保留此版权信息。 12 | * * * =================================== 13 | * * 14 | * * 15 | * 16 | */ 17 | 18 | package com.sunjiajia.monkeyandroid.baselearn.activity; 19 | 20 | import android.content.Intent; 21 | import android.os.Bundle; 22 | import android.support.v7.app.AppCompatActivity; 23 | import android.view.Menu; 24 | import android.view.MenuItem; 25 | import android.view.View; 26 | import android.widget.Button; 27 | import com.sunjiajia.monkeyandroid.R; 28 | import com.sunjiajia.monkeyandroid.customwidgets.activity.CustomWidgetsActivity; 29 | import com.sunjiajia.monkeyandroid.mustlearn.MustLearnPointActivity; 30 | 31 | /** 32 | * 此Activity容纳了基本控件的用法 33 | */ 34 | public class MainActivity extends AppCompatActivity implements View.OnClickListener { 35 | 36 | private Button mBtnLlOnClick; 37 | private Button mBtnRlTl; 38 | private Button mBtnFlGl; 39 | private Button mBtnToast; 40 | private Button mBtnTvEt; 41 | private Button mBtnBtn; 42 | private Button mBtnIv; 43 | private Button mBtnCbRb; 44 | private Button mBtnTbRb; 45 | private Button mBtnSAcTv; 46 | 47 | @Override protected void onCreate(Bundle savedInstanceState) { 48 | super.onCreate(savedInstanceState); 49 | setContentView(R.layout.activity_main); 50 | 51 | initViews(); 52 | configViews(); 53 | } 54 | 55 | @Override public boolean onCreateOptionsMenu(Menu menu) { 56 | getMenuInflater().inflate(R.menu.menu_main, menu); 57 | return true; 58 | } 59 | 60 | @Override public boolean onOptionsItemSelected(MenuItem item) { 61 | int id = item.getItemId(); 62 | if (id == R.id.menu_must_learn_point) { 63 | Intent intent = new Intent(this, MustLearnPointActivity.class); 64 | intent.putExtra("title", item.getTitle()); 65 | startActivity(intent); 66 | } else if (id == R.id.menu_custom_widgets) { 67 | Intent intent = new Intent(this, CustomWidgetsActivity.class); 68 | intent.putExtra("title", item.getTitle()); 69 | startActivity(intent); 70 | } 71 | return super.onOptionsItemSelected(item); 72 | } 73 | 74 | private void initViews() { 75 | mBtnLlOnClick = (Button) findViewById(R.id.btn_ll_onclick); 76 | mBtnRlTl = (Button) findViewById(R.id.btn_rl_tl); 77 | mBtnFlGl = (Button) findViewById(R.id.btn_fl_gl); 78 | mBtnToast = (Button) findViewById(R.id.btn_toast); 79 | mBtnTvEt = (Button) findViewById(R.id.btn_tv_et); 80 | mBtnBtn = (Button) findViewById(R.id.btn_btn); 81 | mBtnIv = (Button) findViewById(R.id.btn_imageview); 82 | mBtnCbRb = (Button) findViewById(R.id.btn_checkbox_radiobutton); 83 | mBtnTbRb = (Button) findViewById(R.id.btn_togglebutton_radiobar); 84 | mBtnSAcTv = (Button) findViewById(R.id.btn_spinner_autocompletetextview); 85 | } 86 | 87 | private void configViews() { 88 | mBtnLlOnClick.setOnClickListener(this); 89 | mBtnRlTl.setOnClickListener(this); 90 | mBtnFlGl.setOnClickListener(this); 91 | mBtnToast.setOnClickListener(this); 92 | mBtnTvEt.setOnClickListener(this); 93 | mBtnBtn.setOnClickListener(this); 94 | mBtnIv.setOnClickListener(this); 95 | mBtnCbRb.setOnClickListener(this); 96 | mBtnTbRb.setOnClickListener(this); 97 | mBtnSAcTv.setOnClickListener(this); 98 | } 99 | 100 | @Override public void onClick(View v) { 101 | String buttonText = (String) ((Button) v).getText(); 102 | myIntent(v, buttonText); 103 | } 104 | 105 | private void myIntent(View view, String text) { 106 | 107 | Intent intent = null; 108 | 109 | switch (view.getId()) { 110 | case R.id.btn_ll_onclick: 111 | intent = new Intent(MainActivity.this, LinearLayoutActivity.class); 112 | break; 113 | case R.id.btn_rl_tl: 114 | intent = new Intent(MainActivity.this, RelativeTableLayoutActivity.class); 115 | break; 116 | case R.id.btn_fl_gl: 117 | intent = new Intent(MainActivity.this, FrameGridLayoutActivity.class); 118 | break; 119 | case R.id.btn_toast: 120 | intent = new Intent(MainActivity.this, ToastActivity.class); 121 | break; 122 | case R.id.btn_tv_et: 123 | intent = new Intent(MainActivity.this, TextViewEditTextActivity.class); 124 | break; 125 | case R.id.btn_btn: 126 | intent = new Intent(MainActivity.this, ButtonActivity.class); 127 | break; 128 | case R.id.btn_imageview: 129 | intent = new Intent(MainActivity.this, ImageViewActivity.class); 130 | break; 131 | case R.id.btn_checkbox_radiobutton: 132 | intent = new Intent(MainActivity.this, CheckBoxRadioButtonActivity.class); 133 | break; 134 | case R.id.btn_togglebutton_radiobar: 135 | intent = new Intent(MainActivity.this, ToggleButtonRadioBarActivity.class); 136 | break; 137 | case R.id.btn_spinner_autocompletetextview: 138 | intent = new Intent(MainActivity.this, SpinnerAutoCompleteTextViewActivity.class); 139 | break; 140 | } 141 | intent.putExtra("title", text); 142 | startActivity(intent); 143 | } 144 | } 145 | -------------------------------------------------------------------------------- /app/src/main/java/com/sunjiajia/monkeyandroid/baselearn/activity/RelativeTableLayoutActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * 4 | * * * =================================== 5 | * * * Copyright (c) 2016. 6 | * * * 作者:安卓猴 7 | * * * 微博:@安卓猴 8 | * * * 博客:http://sunjiajia.com 9 | * * * Github:https://github.com/opengit 10 | * * * 11 | * * * 注意**:如果您使用或者修改该代码,请务必保留此版权信息。 12 | * * * =================================== 13 | * * 14 | * * 15 | * 16 | */ 17 | 18 | package com.sunjiajia.monkeyandroid.baselearn.activity; 19 | 20 | import android.os.Bundle; 21 | import android.support.annotation.Nullable; 22 | import com.sunjiajia.monkeyandroid.R; 23 | 24 | /** 25 | * Created by monkey on 12/6/15. 26 | */ 27 | public class RelativeTableLayoutActivity extends BaseActivity { 28 | @Override public int giveViewResId() { 29 | return R.layout.activity_relative_table_layout; 30 | } 31 | 32 | @Override protected void onCreate(@Nullable Bundle savedInstanceState) { 33 | super.onCreate(savedInstanceState); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /app/src/main/java/com/sunjiajia/monkeyandroid/baselearn/activity/SpinnerAutoCompleteTextViewActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * 4 | * * * =================================== 5 | * * * Copyright (c) 2016. 6 | * * * 作者:安卓猴 7 | * * * 微博:@安卓猴 8 | * * * 博客:http://sunjiajia.com 9 | * * * Github:https://github.com/opengit 10 | * * * 11 | * * * 注意**:如果您使用或者修改该代码,请务必保留此版权信息。 12 | * * * =================================== 13 | * * 14 | * * 15 | * 16 | */ 17 | 18 | package com.sunjiajia.monkeyandroid.baselearn.activity; 19 | 20 | import android.os.Bundle; 21 | import android.support.annotation.Nullable; 22 | import android.widget.ArrayAdapter; 23 | import android.widget.AutoCompleteTextView; 24 | import android.widget.Spinner; 25 | import com.sunjiajia.monkeyandroid.R; 26 | 27 | /** 28 | * Created by monkey on 1/3/16. 29 | */ 30 | public class SpinnerAutoCompleteTextViewActivity extends BaseActivity { 31 | private Spinner mSpinnerCity; 32 | private String[] mDatas; 33 | private AutoCompleteTextView tvAc; 34 | 35 | @Override public int giveViewResId() { 36 | return R.layout.activity_spinner_autocompletetextview; 37 | } 38 | 39 | @Override protected void onCreate(@Nullable Bundle savedInstanceState) { 40 | super.onCreate(savedInstanceState); 41 | intViews(); 42 | initData(); 43 | confingViews(); 44 | } 45 | 46 | private void confingViews() { 47 | setSpinner(); 48 | setAutoCompleteTextView(); 49 | } 50 | 51 | private void setAutoCompleteTextView() { 52 | 53 | ArrayAdapter adapter03 = 54 | ArrayAdapter.createFromResource(this, R.array.av_male, android.R.layout.simple_list_item_1); 55 | tvAc.setAdapter(adapter03); 56 | } 57 | 58 | private void setSpinner() { 59 | //通过适配器来进行动态的数据填充 60 | // 第一种: 61 | /* ArrayAdapter adapter01 = 62 | new ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, mDatas); 63 | mSpinnerCity.setAdapter(adapter01);*/ 64 | 65 | ArrayAdapter adapter02 = 66 | ArrayAdapter.createFromResource(this, R.array.city, android.R.layout.simple_list_item_1); 67 | mSpinnerCity.setAdapter(adapter02); 68 | } 69 | 70 | private void intViews() { 71 | mSpinnerCity = (Spinner) findViewById(R.id.spinner_city); 72 | tvAc = (AutoCompleteTextView) findViewById(R.id.tv_autocomplete); 73 | } 74 | 75 | protected void initData() { 76 | mDatas = new String[] { "动态数据Spinner", "北京", "上海", "深圳", "广州", "洛杉矶", "纽约", "华盛顿" }; 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /app/src/main/java/com/sunjiajia/monkeyandroid/baselearn/activity/TextViewEditTextActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * 4 | * * * =================================== 5 | * * * Copyright (c) 2016. 6 | * * * 作者:安卓猴 7 | * * * 微博:@安卓猴 8 | * * * 博客:http://sunjiajia.com 9 | * * * Github:https://github.com/opengit 10 | * * * 11 | * * * 注意**:如果您使用或者修改该代码,请务必保留此版权信息。 12 | * * * =================================== 13 | * * 14 | * * 15 | * 16 | */ 17 | 18 | package com.sunjiajia.monkeyandroid.baselearn.activity; 19 | 20 | import android.os.Bundle; 21 | import android.support.annotation.Nullable; 22 | import android.text.Editable; 23 | import android.text.TextWatcher; 24 | import android.view.KeyEvent; 25 | import android.widget.EditText; 26 | import android.widget.TextView; 27 | import android.widget.Toast; 28 | import com.sunjiajia.monkeyandroid.R; 29 | 30 | /** 31 | * Created by monkey on 12/8/15. 32 | */ 33 | public class TextViewEditTextActivity extends BaseActivity { 34 | private EditText mEtUname; 35 | private EditText mEtPwd; 36 | 37 | @Override public int giveViewResId() { 38 | return R.layout.activity_textview_edittext; 39 | } 40 | 41 | @Override protected void onCreate(@Nullable Bundle savedInstanceState) { 42 | super.onCreate(savedInstanceState); 43 | 44 | mEtUname = (EditText) findViewById(R.id.et_uname); 45 | mEtPwd = (EditText) findViewById(R.id.et_pwd); 46 | 47 | /** 48 | * EditText中文本输入变化的监听 49 | */ 50 | mEtUname.addTextChangedListener(new TextWatcher() { 51 | @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { 52 | Toast.makeText(TextViewEditTextActivity.this, "beforeTextChanged-->s-->" + s, 53 | Toast.LENGTH_SHORT).show(); 54 | } 55 | 56 | @Override public void onTextChanged(CharSequence s, int start, int before, int count) { 57 | Toast.makeText(TextViewEditTextActivity.this, "onTextChanged-->s-->" + s, 58 | Toast.LENGTH_SHORT).show(); 59 | } 60 | 61 | @Override public void afterTextChanged(Editable s) { 62 | Toast.makeText(TextViewEditTextActivity.this, "afterTextChanged-->s-->" + s, 63 | Toast.LENGTH_SHORT).show(); 64 | } 65 | }); 66 | 67 | /** 68 | * 回车键监听事件 69 | */ 70 | mEtUname.setOnEditorActionListener(new TextView.OnEditorActionListener() { 71 | @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 72 | Toast.makeText(TextViewEditTextActivity.this, "onEditorAction-->" + v.getText(), 73 | Toast.LENGTH_SHORT).show(); 74 | return true; 75 | } 76 | }); 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /app/src/main/java/com/sunjiajia/monkeyandroid/baselearn/activity/ToastActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * 4 | * * * =================================== 5 | * * * Copyright (c) 2016. 6 | * * * 作者:安卓猴 7 | * * * 微博:@安卓猴 8 | * * * 博客:http://sunjiajia.com 9 | * * * Github:https://github.com/opengit 10 | * * * 11 | * * * 注意**:如果您使用或者修改该代码,请务必保留此版权信息。 12 | * * * =================================== 13 | * * 14 | * * 15 | * 16 | */ 17 | 18 | package com.sunjiajia.monkeyandroid.baselearn.activity; 19 | 20 | import android.os.Bundle; 21 | import android.support.annotation.Nullable; 22 | import android.view.Gravity; 23 | import android.view.View; 24 | import android.widget.Button; 25 | import android.widget.ImageView; 26 | import android.widget.LinearLayout; 27 | import android.widget.Toast; 28 | import com.sunjiajia.monkeyandroid.R; 29 | 30 | /** 31 | * Created by monkey on 12/6/15. 32 | */ 33 | public class ToastActivity extends BaseActivity implements View.OnClickListener { 34 | private Button mBtnToast1; 35 | private Button mBtnToast2; 36 | private Button mBtnToast3; 37 | private Button mBtnToast4; 38 | 39 | @Override public int giveViewResId() { 40 | return R.layout.activity_toast; 41 | } 42 | 43 | @Override protected void onCreate(@Nullable Bundle savedInstanceState) { 44 | super.onCreate(savedInstanceState); 45 | 46 | mBtnToast1 = (Button) findViewById(R.id.btn_toast1); 47 | mBtnToast2 = (Button) findViewById(R.id.btn_toast2); 48 | mBtnToast3 = (Button) findViewById(R.id.btn_toast3); 49 | mBtnToast4 = (Button) findViewById(R.id.btn_toast4); 50 | 51 | mBtnToast1.setOnClickListener(this); 52 | mBtnToast2.setOnClickListener(this); 53 | mBtnToast3.setOnClickListener(this); 54 | mBtnToast4.setOnClickListener(this); 55 | } 56 | 57 | @Override public void onClick(View v) { 58 | switch (v.getId()) { 59 | case R.id.btn_toast1: 60 | Toast.makeText(this, "我是短时间土司1号!我的显示时长是Toast.LENGTH_SHORT", Toast.LENGTH_SHORT).show(); 61 | break; 62 | case R.id.btn_toast2: 63 | Toast.makeText(this, "我是长时间土司2号!我的显示时长是Toast.LENGTH_LONG", Toast.LENGTH_LONG).show(); 64 | break; 65 | case R.id.btn_toast3: 66 | // Toast的一个实例 67 | Toast toast3 = new Toast(this); 68 | // 显示图片的控件ImageView,并且给它设置图片 69 | ImageView imageView1 = new ImageView(this); 70 | imageView1.setImageResource(R.mipmap.ic_launcher); 71 | // 将图片控件设置到Toast中 72 | toast3.setView(imageView1); 73 | // 设置Toast的显示时间长短 74 | toast3.setDuration(Toast.LENGTH_SHORT); 75 | // 设置Toast显示的位置 76 | toast3.setGravity(Gravity.LEFT | Gravity.CENTER, 50, 100); 77 | // 让Toast显示出来 78 | toast3.show(); 79 | break; 80 | case R.id.btn_toast4: 81 | 82 | Toast toast4 = Toast.makeText(this, "我是图文土司4号的文字", Toast.LENGTH_SHORT); 83 | // 显示图片的控件ImageView,并且给它设置图片 84 | ImageView imageView2 = new ImageView(this); 85 | imageView2.setImageResource(R.mipmap.ic_launcher); 86 | // 获得Toast的布局 87 | LinearLayout toast4View = (LinearLayout) toast4.getView(); 88 | // 设置此布局为横向的 89 | toast4View.setOrientation(LinearLayout.HORIZONTAL); 90 | // 将图片视图添加到此布局中的第一个位置 91 | toast4View.addView(imageView2, 0); 92 | // 设置Toast显示的位置 93 | toast4.setGravity(Gravity.RIGHT | Gravity.CENTER, 50, 100); 94 | toast4.show(); 95 | break; 96 | } 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /app/src/main/java/com/sunjiajia/monkeyandroid/baselearn/activity/ToggleButtonRadioBarActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * 4 | * * * =================================== 5 | * * * Copyright (c) 2016. 6 | * * * 作者:安卓猴 7 | * * * 微博:@安卓猴 8 | * * * 博客:http://sunjiajia.com 9 | * * * Github:https://github.com/opengit 10 | * * * 11 | * * * 注意**:如果您使用或者修改该代码,请务必保留此版权信息。 12 | * * * =================================== 13 | * * 14 | * * 15 | * 16 | */ 17 | 18 | package com.sunjiajia.monkeyandroid.baselearn.activity; 19 | 20 | import android.os.Bundle; 21 | import android.support.annotation.Nullable; 22 | import android.widget.CompoundButton; 23 | import android.widget.RatingBar; 24 | import android.widget.Switch; 25 | import android.widget.Toast; 26 | import android.widget.ToggleButton; 27 | import com.sunjiajia.monkeyandroid.R; 28 | 29 | /** 30 | * Created by monkey on 1/2/16. 31 | */ 32 | public class ToggleButtonRadioBarActivity extends BaseActivity 33 | implements CompoundButton.OnCheckedChangeListener, RatingBar.OnRatingBarChangeListener { 34 | private ToggleButton mTb; 35 | private Switch mSwitch; 36 | private RatingBar mRb; 37 | 38 | @Override public int giveViewResId() { 39 | return R.layout.activity_togglebutton_radiobar; 40 | } 41 | 42 | @Override protected void onCreate(@Nullable Bundle savedInstanceState) { 43 | super.onCreate(savedInstanceState); 44 | mTb = (ToggleButton) findViewById(R.id.toggle_button); 45 | mSwitch = (Switch) findViewById(R.id.switch_button); 46 | mRb = (RatingBar) findViewById(R.id.rating_bar); 47 | 48 | mTb.setOnCheckedChangeListener(this); 49 | mSwitch.setOnCheckedChangeListener(this); 50 | mRb.setOnRatingBarChangeListener(this); 51 | } 52 | 53 | /** 54 | * ToggleButton和Switch的事件监听 55 | */ 56 | @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 57 | switch (buttonView.getId()) { 58 | case R.id.toggle_button: 59 | showToast(isChecked); 60 | break; 61 | case R.id.switch_button: 62 | showToast(isChecked); 63 | break; 64 | } 65 | } 66 | 67 | private void showToast(boolean isChecked) { 68 | 69 | if (isChecked) { 70 | Toast.makeText(ToggleButtonRadioBarActivity.this, "开", Toast.LENGTH_SHORT).show(); 71 | } else { 72 | Toast.makeText(ToggleButtonRadioBarActivity.this, "关", Toast.LENGTH_SHORT).show(); 73 | } 74 | } 75 | 76 | /** 77 | * RatingBar的事件监听方法 78 | */ 79 | @Override public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) { 80 | Toast.makeText(ToggleButtonRadioBarActivity.this, "获得了#" + rating + "#星好评!", Toast.LENGTH_SHORT) 81 | .show(); 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /app/src/main/java/com/sunjiajia/monkeyandroid/customwidgets/activity/CustomWidgetsActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * 4 | * * * 5 | * * * * =================================== 6 | * * * * Copyright (c) 2016. 7 | * * * * 作者:安卓猴 8 | * * * * 微博:@安卓猴 9 | * * * * 博客:http://sunjiajia.com 10 | * * * * Github:https://github.com/opengit 11 | * * * * 12 | * * * * 注意**:如果您使用或者修改该代码,请务必保留此版权信息。 13 | * * * * =================================== 14 | * * * 15 | * * * 16 | * * 17 | * 18 | */ 19 | 20 | package com.sunjiajia.monkeyandroid.customwidgets.activity; 21 | 22 | import android.content.Intent; 23 | import android.os.Bundle; 24 | import android.support.annotation.Nullable; 25 | import android.support.v7.app.AppCompatActivity; 26 | import android.view.View; 27 | import android.widget.Button; 28 | import com.sunjiajia.monkeyandroid.R; 29 | 30 | /** 31 | * Created by monkey on 16-7-11. 32 | */ 33 | public class CustomWidgetsActivity extends AppCompatActivity { 34 | 35 | private Button mBtnRatioIv; 36 | 37 | @Override protected void onCreate(@Nullable Bundle savedInstanceState) { 38 | super.onCreate(savedInstanceState); 39 | setTitle(getIntent().getStringExtra("title")); 40 | setContentView(R.layout.activity_custom_widgets); 41 | 42 | initViews(); 43 | configViews(); 44 | } 45 | 46 | private void configViews() { 47 | BtnClickedListener mClickedListener = new BtnClickedListener(); 48 | mBtnRatioIv.setOnClickListener(mClickedListener); 49 | } 50 | 51 | private void initViews() { 52 | mBtnRatioIv = (Button) findViewById(R.id.btn_ratio_imageview); 53 | } 54 | 55 | private class BtnClickedListener implements View.OnClickListener { 56 | @Override public void onClick(View view) { 57 | int btnId = view.getId(); 58 | Intent intent = new Intent(CustomWidgetsActivity.this, ShowWidgetsActivity.class); 59 | switch (btnId) { 60 | case R.id.btn_ratio_imageview: 61 | intent.putExtra("frag", btnId); 62 | startActivity(intent); 63 | break; 64 | } 65 | intent.removeExtra("frag"); 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /app/src/main/java/com/sunjiajia/monkeyandroid/customwidgets/activity/ShowWidgetsActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * 4 | * * * 5 | * * * * =================================== 6 | * * * * Copyright (c) 2016. 7 | * * * * 作者:安卓猴 8 | * * * * 微博:@安卓猴 9 | * * * * 博客:http://sunjiajia.com 10 | * * * * Github:https://github.com/opengit 11 | * * * * 12 | * * * * 注意**:如果您使用或者修改该代码,请务必保留此版权信息。 13 | * * * * =================================== 14 | * * * 15 | * * * 16 | * * 17 | * 18 | */ 19 | 20 | package com.sunjiajia.monkeyandroid.customwidgets.activity; 21 | 22 | import android.os.Bundle; 23 | import android.support.annotation.Nullable; 24 | import android.support.v4.app.FragmentManager; 25 | import android.support.v4.app.FragmentTransaction; 26 | import android.support.v7.app.AppCompatActivity; 27 | import com.sunjiajia.monkeyandroid.R; 28 | import com.sunjiajia.monkeyandroid.customwidgets.fragment.WidgetFragment; 29 | 30 | /** 31 | * Created by monkey on 16-7-11. 32 | */ 33 | public class ShowWidgetsActivity extends AppCompatActivity { 34 | 35 | private int fragType; 36 | 37 | @Override protected void onCreate(@Nullable Bundle savedInstanceState) { 38 | super.onCreate(savedInstanceState); 39 | setTitle("自定义控件效果"); 40 | setContentView(R.layout.activity_show_widgets); 41 | 42 | initFragments(); 43 | } 44 | 45 | private void initFragments() { 46 | fragType = getIntent().getIntExtra("frag", 0); 47 | if (fragType > 0) { 48 | 49 | FragmentManager manager = getSupportFragmentManager(); 50 | FragmentTransaction transaction = manager.beginTransaction(); 51 | WidgetFragment fragment = WidgetFragment.newInstance(fragType); 52 | transaction.replace(R.id.frame_widgets, fragment); 53 | transaction.commit(); 54 | } else { 55 | throw new IllegalArgumentException("类型错误,初始化Fragment失败。"); 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /app/src/main/java/com/sunjiajia/monkeyandroid/customwidgets/fragment/WidgetFragment.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * 4 | * * * 5 | * * * * =================================== 6 | * * * * Copyright (c) 2016. 7 | * * * * 作者:安卓猴 8 | * * * * 微博:@安卓猴 9 | * * * * 博客:http://sunjiajia.com 10 | * * * * Github:https://github.com/opengit 11 | * * * * 12 | * * * * 注意**:如果您使用或者修改该代码,请务必保留此版权信息。 13 | * * * * =================================== 14 | * * * 15 | * * * 16 | * * 17 | * 18 | */ 19 | 20 | package com.sunjiajia.monkeyandroid.customwidgets.fragment; 21 | 22 | import android.os.Bundle; 23 | import android.support.annotation.Nullable; 24 | import android.support.v4.app.Fragment; 25 | import android.view.LayoutInflater; 26 | import android.view.View; 27 | import android.view.ViewGroup; 28 | import android.widget.Button; 29 | import android.widget.ImageView.ScaleType; 30 | import butterknife.Bind; 31 | import butterknife.ButterKnife; 32 | import butterknife.OnClick; 33 | import com.sunjiajia.monkeyandroid.R; 34 | import com.sunjiajia.monkeyandroid.customwidgets.widgets.RatioImageView; 35 | 36 | /** 37 | * Created by monkey on 16-7-12. 38 | */ 39 | public class WidgetFragment extends Fragment { 40 | 41 | @Bind(R.id.ratio_imageview) RatioImageView mRatioImageview; 42 | @Bind(R.id.btn_sixteen_nine) Button mBtnSixteenNine; 43 | 44 | public static WidgetFragment fragment; 45 | private View mView; 46 | 47 | public static WidgetFragment newInstance(int fragType) { 48 | 49 | if (fragment == null) { 50 | fragment = new WidgetFragment(); 51 | } 52 | 53 | Bundle bundle = new Bundle(); 54 | bundle.putInt("fragType", fragType); 55 | fragment.setArguments(bundle); 56 | return fragment; 57 | } 58 | 59 | @Nullable @Override 60 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, 61 | @Nullable Bundle savedInstanceState) { 62 | 63 | int fragType = getArguments().getInt("fragType"); 64 | int viewResId = 0; 65 | switch (fragType) { 66 | case R.id.btn_ratio_imageview: 67 | viewResId = R.layout.frag_ratio_imageview; 68 | break; 69 | } 70 | 71 | if (viewResId <= 0) { 72 | throw new IllegalArgumentException("Fragment需要填充的View的布局文件Id参数错误。"); 73 | } 74 | 75 | mView = inflater.inflate(viewResId, container, false); 76 | ButterKnife.bind(this, mView); 77 | return mView; 78 | } 79 | 80 | @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { 81 | super.onActivityCreated(savedInstanceState); 82 | 83 | mRatioImageview.setRatio(1, 1); 84 | } 85 | 86 | @Override public void onDestroyView() { 87 | super.onDestroyView(); 88 | ButterKnife.unbind(this); 89 | } 90 | 91 | @OnClick({ 92 | R.id.btn_sixteen_nine 93 | }) public void onClick(View view) { 94 | switch (view.getId()) { 95 | case R.id.btn_sixteen_nine: 96 | mRatioImageview.setRatio(16, 9); 97 | break; 98 | } 99 | mRatioImageview.setScaleType(ScaleType.FIT_XY); 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /app/src/main/java/com/sunjiajia/monkeyandroid/customwidgets/widgets/RatioImageView.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * 4 | * * * 5 | * * * * =================================== 6 | * * * * Copyright (c) 2016. 7 | * * * * 作者:安卓猴 8 | * * * * 微博:@安卓猴 9 | * * * * 博客:http://sunjiajia.com 10 | * * * * Github:https://github.com/opengit 11 | * * * * 12 | * * * * 注意**:如果您使用或者修改该代码,请务必保留此版权信息。 13 | * * * * =================================== 14 | * * * 15 | * * * 16 | * * 17 | * 18 | */ 19 | 20 | package com.sunjiajia.monkeyandroid.customwidgets.widgets; 21 | 22 | import android.content.Context; 23 | import android.util.AttributeSet; 24 | import android.widget.ImageView; 25 | 26 | /** 27 | * Created by monkey on 16-7-11. 28 | */ 29 | public class RatioImageView extends ImageView { 30 | 31 | private int mWidthRatio; 32 | private int mHeightRatio; 33 | 34 | public RatioImageView(Context context) { 35 | this(context, null); 36 | } 37 | 38 | public RatioImageView(Context context, AttributeSet attrs) { 39 | this(context, attrs, 0); 40 | } 41 | 42 | public RatioImageView(Context context, AttributeSet attrs, int defStyleAttr) { 43 | super(context, attrs, defStyleAttr); 44 | } 45 | 46 | /** 47 | * 初始化好RatioImageView控件后,用这个方法来设置宽高的比例,例如:16:9,那么widthRatio就是16,heightRatio就是9 48 | * @param widthRatio 49 | * 宽在比例中占得数值,例如16:9,它的值就是16 50 | * @param heightRatio 51 | * 高在比例中占得数值,例如16:9,它的值就是9 52 | */ 53 | public void setRatio(int widthRatio, int heightRatio) { 54 | if (widthRatio > 0 && heightRatio > 0) { 55 | mWidthRatio = widthRatio; 56 | mHeightRatio = heightRatio; 57 | } else { 58 | throw new IllegalArgumentException( 59 | "the parameter of setRatio(int,int) must be positive Integer!"); 60 | } 61 | } 62 | 63 | @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 64 | 65 | int width = MeasureSpec.getSize(widthMeasureSpec); 66 | int height = MeasureSpec.getSize(heightMeasureSpec); 67 | 68 | double ratio = (double) mWidthRatio / mHeightRatio; 69 | if (width > 0) { 70 | if (mWidthRatio >= mHeightRatio) { 71 | height = (int) (width / ratio + 0.5f); 72 | } else { 73 | width = (int) (height * ratio + 0.5f); 74 | } 75 | setMeasuredDimension(width, height); 76 | 77 | System.out.println("width->" + width + ",height->" + height + ",ratio->" + ratio); 78 | } else { 79 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); 80 | } 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /app/src/main/java/com/sunjiajia/monkeyandroid/domain/Message.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * 4 | * * * =================================== 5 | * * * Copyright (c) 2016. 6 | * * * 作者:安卓猴 7 | * * * 微博:@安卓猴 8 | * * * 博客:http://sunjiajia.com 9 | * * * Github:https://github.com/opengit 10 | * * * 11 | * * * 注意**:如果您使用或者修改该代码,请务必保留此版权信息。 12 | * * * =================================== 13 | * * 14 | * * 15 | * 16 | */ 17 | 18 | package com.sunjiajia.monkeyandroid.domain; 19 | 20 | public class Message { 21 | 22 | private String body; 23 | private String date; 24 | private String address; 25 | private int type; 26 | 27 | public Message() { 28 | super(); 29 | } 30 | 31 | public Message(String body, String date, String address, int type) { 32 | super(); 33 | this.body = body; 34 | this.date = date; 35 | this.address = address; 36 | this.type = type; 37 | } 38 | 39 | public String getBody() { 40 | return body; 41 | } 42 | 43 | public void setBody(String body) { 44 | this.body = body; 45 | } 46 | 47 | public String getDate() { 48 | return date; 49 | } 50 | 51 | public void setDate(String date) { 52 | this.date = date; 53 | } 54 | 55 | public String getAddress() { 56 | return address; 57 | } 58 | 59 | public void setAddress(String address) { 60 | this.address = address; 61 | } 62 | 63 | public int getType() { 64 | return type; 65 | } 66 | 67 | public void setType(int type) { 68 | this.type = type; 69 | } 70 | 71 | @Override public String toString() { 72 | return "Message [body=" 73 | + body 74 | + ", date=" 75 | + date 76 | + ", address=" 77 | + address 78 | + ", type=" 79 | + type 80 | + "]"; 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /app/src/main/java/com/sunjiajia/monkeyandroid/mustlearn/activitydata/FirstActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * 4 | * * * 5 | * * * * =================================== 6 | * * * * Copyright (c) 2016. 7 | * * * * 作者:安卓猴 8 | * * * * 微博:@安卓猴 9 | * * * * 博客:http://sunjiajia.com 10 | * * * * Github:https://github.com/opengit 11 | * * * * 12 | * * * * 注意**:如果您使用或者修改该代码,请务必保留此版权信息。 13 | * * * * =================================== 14 | * * * 15 | * * * 16 | * * 17 | * 18 | */ 19 | 20 | package com.sunjiajia.monkeyandroid.mustlearn.activitydata; 21 | 22 | import android.content.Intent; 23 | import android.os.Bundle; 24 | import android.support.v7.app.ActionBar; 25 | import android.support.v7.app.AppCompatActivity; 26 | import android.view.View; 27 | import android.widget.Button; 28 | import android.widget.EditText; 29 | import butterknife.Bind; 30 | import butterknife.ButterKnife; 31 | import butterknife.OnClick; 32 | import com.sunjiajia.monkeyandroid.R; 33 | 34 | public class FirstActivity extends AppCompatActivity { 35 | 36 | @Bind(R.id.et_male_name) EditText mEtMaleName; 37 | @Bind(R.id.et_female_name) EditText mEtFemaleName; 38 | @Bind(R.id.btn_make1) Button mBtnMake; 39 | @Bind(R.id.btn_make2) Button mBtnMake2; 40 | 41 | @Override protected void onCreate(Bundle savedInstanceState) { 42 | super.onCreate(savedInstanceState); 43 | setContentView(R.layout.activity_first); 44 | ButterKnife.bind(this); 45 | ActionBar actionBar = getSupportActionBar(); 46 | actionBar.setTitle("FirstActivity"); 47 | } 48 | 49 | @OnClick({ R.id.btn_make1, R.id.btn_make2 }) public void onClick(View view) { 50 | 51 | switch (view.getId()) { 52 | case R.id.btn_make1: 53 | Intent intent = new Intent(this, TwoActivity.class); 54 | intent.putExtra("malename", "吕布"); 55 | intent.putExtra("femalename", "貂蝉"); 56 | startActivity(intent); 57 | break; 58 | case R.id.btn_make2: 59 | Bundle bundle = new Bundle(); 60 | bundle.putString("malename2", "唐明皇"); 61 | bundle.putString("femalename2", "杨贵妃"); 62 | Intent intent2 = new Intent(this, TwoActivity.class); 63 | intent2.putExtras(bundle); 64 | startActivity(intent2); 65 | break; 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /app/src/main/java/com/sunjiajia/monkeyandroid/mustlearn/activitydata/TwoActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * 4 | * * * 5 | * * * * =================================== 6 | * * * * Copyright (c) 2016. 7 | * * * * 作者:安卓猴 8 | * * * * 微博:@安卓猴 9 | * * * * 博客:http://sunjiajia.com 10 | * * * * Github:https://github.com/opengit 11 | * * * * 12 | * * * * 注意**:如果您使用或者修改该代码,请务必保留此版权信息。 13 | * * * * =================================== 14 | * * * 15 | * * * 16 | * * 17 | * 18 | */ 19 | 20 | package com.sunjiajia.monkeyandroid.mustlearn.activitydata; 21 | 22 | import android.content.Intent; 23 | import android.os.Bundle; 24 | import android.support.v7.app.ActionBar; 25 | import android.support.v7.app.AppCompatActivity; 26 | import android.widget.TextView; 27 | import butterknife.Bind; 28 | import butterknife.ButterKnife; 29 | import com.sunjiajia.monkeyandroid.R; 30 | import java.util.Random; 31 | 32 | public class TwoActivity extends AppCompatActivity { 33 | 34 | @Bind(R.id.tv_yinyuan) TextView mTvYinyuan; 35 | 36 | @Override protected void onCreate(Bundle savedInstanceState) { 37 | super.onCreate(savedInstanceState); 38 | setContentView(R.layout.activity_two); 39 | ButterKnife.bind(this); 40 | ActionBar actionBar = getSupportActionBar(); 41 | actionBar.setTitle("TwoActivity"); 42 | 43 | // 获取intent中的数据 44 | Intent intent = getIntent(); 45 | String maleName = intent.getStringExtra("malename"); 46 | String femaleName = intent.getStringExtra("femalename"); 47 | 48 | Random random = new Random(); 49 | int yinyuan1 = random.nextInt(100); 50 | 51 | Bundle bundle = intent.getExtras(); 52 | String male2 = bundle.getString("malename2"); 53 | String female2 = bundle.getString("femalename2"); 54 | int yinyuan2 = random.nextInt(100); 55 | 56 | mTvYinyuan.setText(maleName 57 | + "和" 58 | + femaleName 59 | + "的姻缘值为 " 60 | + yinyuan1 61 | + ",\n" 62 | + male2 63 | + "和" 64 | + female2 65 | + "的姻缘值为 " 66 | + yinyuan2); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /app/src/main/java/com/sunjiajia/monkeyandroid/mustlearn/broadcastreceiverservicedemo/BcrActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * 4 | * * * 5 | * * * * =================================== 6 | * * * * Copyright (c) 2016. 7 | * * * * 作者:安卓猴 8 | * * * * 微博:@安卓猴 9 | * * * * 博客:http://sunjiajia.com 10 | * * * * Github:https://github.com/opengit 11 | * * * * 12 | * * * * 注意**:如果您使用或者修改该代码,请务必保留此版权信息。 13 | * * * * =================================== 14 | * * * 15 | * * * 16 | * * 17 | * 18 | */ 19 | 20 | package com.sunjiajia.monkeyandroid.mustlearn.broadcastreceiverservicedemo; 21 | 22 | import android.content.Intent; 23 | import android.content.SharedPreferences; 24 | import android.os.Bundle; 25 | import android.support.v7.app.AppCompatActivity; 26 | import android.view.View; 27 | import android.widget.Button; 28 | import android.widget.Toast; 29 | import butterknife.Bind; 30 | import butterknife.ButterKnife; 31 | import butterknife.OnClick; 32 | import com.sunjiajia.monkeyandroid.R; 33 | 34 | public class BcrActivity extends AppCompatActivity { 35 | 36 | @Bind(R.id.btn_ip_dialer) Button mBtnIpDialer; 37 | @Bind(R.id.btn_sms_blocker) Button mBtnSmsBlocker; 38 | @Bind(R.id.btn_open_service) Button mBtnOpenService; 39 | @Bind(R.id.btn_close_service) Button mBtnCloseService; 40 | 41 | @Override protected void onCreate(Bundle savedInstanceState) { 42 | super.onCreate(savedInstanceState); 43 | setContentView(R.layout.activity_bcr); 44 | ButterKnife.bind(this); 45 | getSupportActionBar().setTitle("广播接收者与服务示例"); 46 | } 47 | 48 | @OnClick({ 49 | R.id.btn_ip_dialer, R.id.btn_sms_blocker, R.id.btn_open_service, R.id.btn_close_service 50 | }) public void onClick(View view) { 51 | switch (view.getId()) { 52 | case R.id.btn_ip_dialer: 53 | 54 | SharedPreferences sp = getSharedPreferences("ip", MODE_PRIVATE); 55 | SharedPreferences.Editor editor = sp.edit(); 56 | editor.putString("ip", "101010"); 57 | editor.commit(); 58 | Toast.makeText(this, "ip前缀设置完毕,拨打所有电话将更加便宜,\n请去拨号!", Toast.LENGTH_SHORT).show(); 59 | 60 | break; 61 | case R.id.btn_sms_blocker: 62 | 63 | Toast.makeText(this, "拦截的短信号码为168168,请用这个号码在模拟发送短信!", Toast.LENGTH_SHORT).show(); 64 | 65 | break; 66 | case R.id.btn_open_service: 67 | 68 | // 显式启动服务 69 | startService(new Intent(this, MyService.class)); 70 | 71 | break; 72 | case R.id.btn_close_service: 73 | // 关闭服务 74 | stopService(new Intent(this, MyService.class)); 75 | break; 76 | } 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /app/src/main/java/com/sunjiajia/monkeyandroid/mustlearn/broadcastreceiverservicedemo/IpCallReceiver.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * 4 | * * * 5 | * * * * =================================== 6 | * * * * Copyright (c) 2016. 7 | * * * * 作者:安卓猴 8 | * * * * 微博:@安卓猴 9 | * * * * 博客:http://sunjiajia.com 10 | * * * * Github:https://github.com/opengit 11 | * * * * 12 | * * * * 注意**:如果您使用或者修改该代码,请务必保留此版权信息。 13 | * * * * =================================== 14 | * * * 15 | * * * 16 | * * 17 | * 18 | */ 19 | 20 | package com.sunjiajia.monkeyandroid.mustlearn.broadcastreceiverservicedemo; 21 | 22 | import android.content.BroadcastReceiver; 23 | import android.content.Context; 24 | import android.content.Intent; 25 | import android.content.SharedPreferences; 26 | 27 | /** 28 | * Created by monkey on 16-5-6. 29 | * 30 | * 拨打电话加ip前缀的广播接收者 31 | */ 32 | public class IpCallReceiver extends BroadcastReceiver { 33 | /** 34 | * 接受到广播的时候调用 35 | * @param context 36 | * @param intent 37 | */ 38 | @Override public void onReceive(Context context, Intent intent) { 39 | System.out.println("我是广播接收者"); 40 | // 在打电话广播中,会携带拨打的电话的号码,我们获取到它 41 | String phone = getResultData(); 42 | System.out.println("电话号码->" + phone); 43 | 44 | SharedPreferences sp = context.getSharedPreferences("ip", Context.MODE_PRIVATE); 45 | String ip = sp.getString("ip", "-"); 46 | 47 | String newPhone = ip + phone; 48 | System.out.println("带有ip的电话号码->" + newPhone); 49 | 50 | // 把新的号码重新放入广播中 51 | setResultData(newPhone); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /app/src/main/java/com/sunjiajia/monkeyandroid/mustlearn/broadcastreceiverservicedemo/MyService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * 4 | * * * 5 | * * * * =================================== 6 | * * * * Copyright (c) 2016. 7 | * * * * 作者:安卓猴 8 | * * * * 微博:@安卓猴 9 | * * * * 博客:http://sunjiajia.com 10 | * * * * Github:https://github.com/opengit 11 | * * * * 12 | * * * * 注意**:如果您使用或者修改该代码,请务必保留此版权信息。 13 | * * * * =================================== 14 | * * * 15 | * * * 16 | * * 17 | * 18 | */ 19 | 20 | package com.sunjiajia.monkeyandroid.mustlearn.broadcastreceiverservicedemo; 21 | 22 | import android.app.Service; 23 | import android.content.Intent; 24 | import android.os.IBinder; 25 | 26 | public class MyService extends Service { 27 | 28 | @Override public IBinder onBind(Intent intent) { 29 | 30 | return null; 31 | } 32 | 33 | @Override public void onCreate() { 34 | super.onCreate(); 35 | System.out.println("MyService-->onCreate"); 36 | } 37 | 38 | @Override public void onDestroy() { 39 | super.onDestroy(); 40 | System.out.println("MyService-->onDestroy"); 41 | } 42 | 43 | @Override public int onStartCommand(Intent intent, int flags, int startId) { 44 | System.out.println("MyService-->onStartCommand"); 45 | return super.onStartCommand(intent, flags, startId); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /app/src/main/java/com/sunjiajia/monkeyandroid/mustlearn/broadcastreceiverservicedemo/SmsReceiver.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * 4 | * * * 5 | * * * * =================================== 6 | * * * * Copyright (c) 2016. 7 | * * * * 作者:安卓猴 8 | * * * * 微博:@安卓猴 9 | * * * * 博客:http://sunjiajia.com 10 | * * * * Github:https://github.com/opengit 11 | * * * * 12 | * * * * 注意**:如果您使用或者修改该代码,请务必保留此版权信息。 13 | * * * * =================================== 14 | * * * 15 | * * * 16 | * * 17 | * 18 | */ 19 | 20 | package com.sunjiajia.monkeyandroid.mustlearn.broadcastreceiverservicedemo; 21 | 22 | import android.content.BroadcastReceiver; 23 | import android.content.Context; 24 | import android.content.Intent; 25 | import android.os.Bundle; 26 | import android.telephony.SmsMessage; 27 | 28 | /** 29 | * Created by monkey on 16-5-6. 30 | * 垃圾短信广播接收者 31 | * 32 | * 测试短信号码:168168 33 | * 测试短信内容:你好,安卓猴 34 | */ 35 | public class SmsReceiver extends BroadcastReceiver { 36 | private static final String BLOCK_PHONE = "168168"; 37 | 38 | @Override public void onReceive(Context context, Intent intent) { 39 | System.out.println("收到短信了。。。"); 40 | // 你好美女 41 | // 拿到短信的内容,它是封装在intent中的 42 | Bundle bundle = intent.getExtras(); 43 | // object数组中的每一个元素都是一条短信 44 | Object[] objects = (Object[]) bundle.get("pdus"); 45 | for (Object object : objects) { 46 | SmsMessage message = SmsMessage.createFromPdu((byte[]) object); 47 | String address = message.getOriginatingAddress(); 48 | String body = message.getMessageBody(); 49 | 50 | System.out.println(address + " --> " + body); 51 | 52 | if (address.equals(BLOCK_PHONE)) { 53 | // 如果短信号码等于黑名单号码,则停止广播 54 | abortBroadcast(); 55 | } 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /app/src/main/java/com/sunjiajia/monkeyandroid/mustlearn/dagger2/Dagger2Activity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * 4 | * * * 5 | * * * * =================================== 6 | * * * * Copyright (c) 2016. 7 | * * * * 作者:安卓猴 8 | * * * * 微博:@安卓猴 9 | * * * * 博客:http://sunjiajia.com 10 | * * * * Github:https://github.com/opengit 11 | * * * * 12 | * * * * 注意**:如果您使用或者修改该代码,请务必保留此版权信息。 13 | * * * * =================================== 14 | * * * 15 | * * * 16 | * * 17 | * 18 | */ 19 | 20 | package com.sunjiajia.monkeyandroid.mustlearn.dagger2; 21 | 22 | import android.os.Bundle; 23 | import android.support.annotation.Nullable; 24 | import android.support.v7.app.AppCompatActivity; 25 | import android.widget.ListView; 26 | import butterknife.Bind; 27 | import butterknife.ButterKnife; 28 | import com.sunjiajia.monkeyandroid.R; 29 | import com.sunjiajia.monkeyandroid.mustlearn.dagger2.adapter.UserAdapter; 30 | import com.sunjiajia.monkeyandroid.mustlearn.dagger2.injection.component.DaggerUserComponent; 31 | import com.sunjiajia.monkeyandroid.mustlearn.dagger2.injection.modules.UserModule; 32 | import javax.inject.Inject; 33 | 34 | /** 35 | * Created by Administrator on 2016/4/21. 36 | */ 37 | public class Dagger2Activity extends AppCompatActivity { 38 | 39 | @Bind(R.id.list_view) ListView mListView; 40 | 41 | @Inject UserAdapter mAdapter; 42 | 43 | @Override protected void onCreate(@Nullable Bundle savedInstanceState) { 44 | super.onCreate(savedInstanceState); 45 | setContentView(R.layout.activity_dagger2); 46 | ButterKnife.bind(this); 47 | 48 | DaggerUserComponent.builder().userModule(new UserModule(this)).build().inject22(this); 49 | 50 | mListView.setAdapter(mAdapter); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /app/src/main/java/com/sunjiajia/monkeyandroid/mustlearn/dagger2/adapter/UserAdapter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * 4 | * * * 5 | * * * * =================================== 6 | * * * * Copyright (c) 2016. 7 | * * * * 作者:安卓猴 8 | * * * * 微博:@安卓猴 9 | * * * * 博客:http://sunjiajia.com 10 | * * * * Github:https://github.com/opengit 11 | * * * * 12 | * * * * 注意**:如果您使用或者修改该代码,请务必保留此版权信息。 13 | * * * * =================================== 14 | * * * 15 | * * * 16 | * * 17 | * 18 | */ 19 | 20 | package com.sunjiajia.monkeyandroid.mustlearn.dagger2.adapter; 21 | 22 | import android.content.Context; 23 | import android.view.LayoutInflater; 24 | import android.view.View; 25 | import android.view.ViewGroup; 26 | import android.widget.BaseAdapter; 27 | import android.widget.TextView; 28 | import android.widget.Toast; 29 | import butterknife.Bind; 30 | import butterknife.ButterKnife; 31 | import com.sunjiajia.monkeyandroid.R; 32 | import java.util.List; 33 | import javax.inject.Inject; 34 | 35 | /** 36 | * Created by monkey on 16-4-26. 37 | */ 38 | public class UserAdapter extends BaseAdapter { 39 | private LayoutInflater mInflater; 40 | private List mUserList; 41 | private Context mContext; 42 | 43 | @Inject public UserAdapter(Context context, List list) { 44 | this.mInflater = LayoutInflater.from(context); 45 | this.mUserList = list; 46 | this.mContext = context; 47 | } 48 | 49 | @Override public int getCount() { 50 | return mUserList.size(); 51 | } 52 | 53 | @Override public Object getItem(int position) { 54 | return mUserList.get(position); 55 | } 56 | 57 | @Override public long getItemId(int position) { 58 | return position; 59 | } 60 | 61 | @Override public View getView(final int position, View convertView, ViewGroup parent) { 62 | 63 | MyViewHolder holder; 64 | 65 | if (convertView == null) { 66 | convertView = mInflater.inflate(R.layout.item_user, parent, false); 67 | holder = new MyViewHolder(convertView); 68 | convertView.setTag(holder); 69 | } else { 70 | holder = (MyViewHolder) convertView.getTag(); 71 | } 72 | 73 | holder.mTvUser.setText(mUserList.get(position)); 74 | holder.mTvUser.setOnClickListener(new View.OnClickListener() { 75 | @Override public void onClick(View v) { 76 | Toast.makeText(mContext, mUserList.get(position), Toast.LENGTH_SHORT).show(); 77 | } 78 | }); 79 | 80 | return convertView; 81 | } 82 | 83 | static class MyViewHolder { 84 | @Bind(R.id.tv_user) TextView mTvUser; 85 | 86 | MyViewHolder(View view) { 87 | ButterKnife.bind(this, view); 88 | } 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /app/src/main/java/com/sunjiajia/monkeyandroid/mustlearn/dagger2/injection/ActivityScope.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * 4 | * * * 5 | * * * * =================================== 6 | * * * * Copyright (c) 2016. 7 | * * * * 作者:安卓猴 8 | * * * * 微博:@安卓猴 9 | * * * * 博客:http://sunjiajia.com 10 | * * * * Github:https://github.com/opengit 11 | * * * * 12 | * * * * 注意**:如果您使用或者修改该代码,请务必保留此版权信息。 13 | * * * * =================================== 14 | * * * 15 | * * * 16 | * * 17 | * 18 | */ 19 | 20 | package com.sunjiajia.monkeyandroid.mustlearn.dagger2.injection; 21 | 22 | import java.lang.annotation.Retention; 23 | import java.lang.annotation.RetentionPolicy; 24 | import javax.inject.Scope; 25 | 26 | /** 27 | * Created by monkey on 16-4-26. 28 | */ 29 | @Retention(RetentionPolicy.RUNTIME) @Scope public @interface ActivityScope { 30 | } 31 | -------------------------------------------------------------------------------- /app/src/main/java/com/sunjiajia/monkeyandroid/mustlearn/dagger2/injection/component/UserComponent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * 4 | * * * 5 | * * * * =================================== 6 | * * * * Copyright (c) 2016. 7 | * * * * 作者:安卓猴 8 | * * * * 微博:@安卓猴 9 | * * * * 博客:http://sunjiajia.com 10 | * * * * Github:https://github.com/opengit 11 | * * * * 12 | * * * * 注意**:如果您使用或者修改该代码,请务必保留此版权信息。 13 | * * * * =================================== 14 | * * * 15 | * * * 16 | * * 17 | * 18 | */ 19 | 20 | package com.sunjiajia.monkeyandroid.mustlearn.dagger2.injection.component; 21 | 22 | import com.sunjiajia.monkeyandroid.mustlearn.dagger2.Dagger2Activity; 23 | import com.sunjiajia.monkeyandroid.mustlearn.dagger2.injection.ActivityScope; 24 | import com.sunjiajia.monkeyandroid.mustlearn.dagger2.injection.modules.UserModule; 25 | import dagger.Component; 26 | 27 | /** 28 | * Created by monkey on 16-4-26. 29 | */ 30 | @ActivityScope @Component(modules = { UserModule.class }) public interface UserComponent { 31 | void inject22(Dagger2Activity activity); 32 | } 33 | -------------------------------------------------------------------------------- /app/src/main/java/com/sunjiajia/monkeyandroid/mustlearn/dagger2/injection/modules/UserModule.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * 4 | * * * 5 | * * * * =================================== 6 | * * * * Copyright (c) 2016. 7 | * * * * 作者:安卓猴 8 | * * * * 微博:@安卓猴 9 | * * * * 博客:http://sunjiajia.com 10 | * * * * Github:https://github.com/opengit 11 | * * * * 12 | * * * * 注意**:如果您使用或者修改该代码,请务必保留此版权信息。 13 | * * * * =================================== 14 | * * * 15 | * * * 16 | * * 17 | * 18 | */ 19 | 20 | package com.sunjiajia.monkeyandroid.mustlearn.dagger2.injection.modules; 21 | 22 | import android.content.Context; 23 | import com.sunjiajia.monkeyandroid.mustlearn.dagger2.injection.ActivityScope; 24 | import dagger.Module; 25 | import dagger.Provides; 26 | import java.util.ArrayList; 27 | import java.util.List; 28 | 29 | /** 30 | * Created by monkey on 16-4-26. 31 | */ 32 | @Module public class UserModule { 33 | 34 | private static final int USER_COUNT = 100; 35 | 36 | private Context context; 37 | 38 | public UserModule(Context context) { 39 | this.context = context; 40 | } 41 | 42 | @ActivityScope @Provides Context provideContext() { 43 | return context; 44 | } 45 | 46 | @ActivityScope @Provides List provideUsers() { 47 | List users = new ArrayList<>(USER_COUNT); 48 | for (int i = 0; i < USER_COUNT; i++) { 49 | users.add("user:Jack#" + i); 50 | } 51 | return users; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /app/src/main/java/com/sunjiajia/monkeyandroid/mustlearn/databinding/CapitalUtil.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * 4 | * * * 5 | * * * * =================================== 6 | * * * * Copyright (c) 2016. 7 | * * * * 作者:安卓猴 8 | * * * * 微博:@安卓猴 9 | * * * * 博客:http://sunjiajia.com 10 | * * * * Github:https://github.com/opengit 11 | * * * * 12 | * * * * 注意**:如果您使用或者修改该代码,请务必保留此版权信息。 13 | * * * * =================================== 14 | * * * 15 | * * * 16 | * * 17 | * 18 | */ 19 | 20 | package com.sunjiajia.monkeyandroid.mustlearn.databinding; 21 | 22 | /** 23 | * Created by monkey on 16-7-14. 24 | */ 25 | public class CapitalUtil { 26 | public static final String upToCapital(String str) { 27 | return str.toUpperCase(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /app/src/main/java/com/sunjiajia/monkeyandroid/mustlearn/databinding/DataBindingActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * 4 | * * * 5 | * * * * =================================== 6 | * * * * Copyright (c) 2016. 7 | * * * * 作者:安卓猴 8 | * * * * 微博:@安卓猴 9 | * * * * 博客:http://sunjiajia.com 10 | * * * * Github:https://github.com/opengit 11 | * * * * 12 | * * * * 注意**:如果您使用或者修改该代码,请务必保留此版权信息。 13 | * * * * =================================== 14 | * * * 15 | * * * 16 | * * 17 | * 18 | */ 19 | 20 | package com.sunjiajia.monkeyandroid.mustlearn.databinding; 21 | 22 | import android.databinding.DataBindingUtil; 23 | import android.os.Bundle; 24 | import android.support.v7.app.AppCompatActivity; 25 | import android.support.v7.widget.LinearLayoutManager; 26 | import com.sunjiajia.monkeyandroid.R; 27 | import com.sunjiajia.monkeyandroid.databinding.ActivityDataBindingBinding; 28 | import com.sunjiajia.monkeyandroid.mustlearn.databinding.adapter.RecyclerAdapter; 29 | import com.sunjiajia.monkeyandroid.mustlearn.databinding.bean.User; 30 | 31 | /** 32 | * 这个Activity我们来学习DataBinding的用法,关于教程,这里给出一个链接,可以跟着边看文章遍阅读这个示例,理解更深。 33 | * 34 | * https://github.com/LyndonChin/MasteringAndroidDataBinding/blob/master/README.md 35 | */ 36 | public class DataBindingActivity extends AppCompatActivity { 37 | 38 | @Override protected void onCreate(Bundle savedInstanceState) { 39 | super.onCreate(savedInstanceState); 40 | setTitle(getIntent().getStringExtra("title")); 41 | //setContentView(R.layout.activity_data_binding); 42 | ActivityDataBindingBinding binding = 43 | DataBindingUtil.setContentView(this, R.layout.activity_data_binding); 44 | User user = new User("Jack", "123456", 18, "jack@jack.com", false); 45 | user.setVip(true); 46 | binding.setUser(user); 47 | 48 | binding.recycler.setLayoutManager(new LinearLayoutManager(this)); 49 | binding.recycler.setHasFixedSize(true); 50 | binding.recycler.setAdapter(new RecyclerAdapter()); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /app/src/main/java/com/sunjiajia/monkeyandroid/mustlearn/databinding/adapter/RecyclerAdapter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * 4 | * * * 5 | * * * * =================================== 6 | * * * * Copyright (c) 2016. 7 | * * * * 作者:安卓猴 8 | * * * * 微博:@安卓猴 9 | * * * * 博客:http://sunjiajia.com 10 | * * * * Github:https://github.com/opengit 11 | * * * * 12 | * * * * 注意**:如果您使用或者修改该代码,请务必保留此版权信息。 13 | * * * * =================================== 14 | * * * 15 | * * * 16 | * * 17 | * 18 | */ 19 | 20 | package com.sunjiajia.monkeyandroid.mustlearn.databinding.adapter; 21 | 22 | import android.databinding.DataBindingUtil; 23 | import android.databinding.ViewDataBinding; 24 | import android.support.annotation.NonNull; 25 | import android.support.v7.widget.RecyclerView; 26 | import android.view.LayoutInflater; 27 | import android.view.View; 28 | import android.view.ViewGroup; 29 | import com.android.databinding.library.baseAdapters.BR; 30 | import com.sunjiajia.monkeyandroid.R; 31 | import com.sunjiajia.monkeyandroid.mustlearn.databinding.bean.User; 32 | import java.util.ArrayList; 33 | 34 | /** 35 | * Created by monkey on 16-7-14. 36 | */ 37 | public class RecyclerAdapter extends RecyclerView.Adapter { 38 | 39 | private static final int ITEM_COUNT = 50; 40 | 41 | @NonNull private ArrayList mList; 42 | 43 | public RecyclerAdapter() { 44 | mList = new ArrayList<>(ITEM_COUNT); 45 | for (int i = 0; i < ITEM_COUNT; i++) { 46 | User user = new User("Jack # " + i, "123456" + i, 18 + i, "Jack # " + i + "@jack.com", false); 47 | mList.add(user); 48 | } 49 | } 50 | 51 | @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 52 | ViewDataBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), 53 | R.layout.item_recycler_binding, parent, false); 54 | MyViewHolder holder = new MyViewHolder(binding.getRoot()); 55 | holder.setBinding(binding); 56 | return holder; 57 | } 58 | 59 | @Override public void onBindViewHolder(MyViewHolder holder, int position) { 60 | User user = mList.get(position); 61 | System.out.println(user.toString()); 62 | holder.getBinding().setVariable(BR.user, user); 63 | } 64 | 65 | @Override public int getItemCount() { 66 | return mList.size(); 67 | } 68 | 69 | class MyViewHolder extends RecyclerView.ViewHolder { 70 | 71 | private ViewDataBinding mBinding; 72 | 73 | public ViewDataBinding getBinding() { 74 | return mBinding; 75 | } 76 | 77 | public void setBinding(ViewDataBinding binding) { 78 | this.mBinding = binding; 79 | } 80 | 81 | public MyViewHolder(View itemView) { 82 | super(itemView); 83 | } 84 | } 85 | } 86 | 87 | -------------------------------------------------------------------------------- /app/src/main/java/com/sunjiajia/monkeyandroid/mustlearn/databinding/bean/User.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * 4 | * * * 5 | * * * * =================================== 6 | * * * * Copyright (c) 2016. 7 | * * * * 作者:安卓猴 8 | * * * * 微博:@安卓猴 9 | * * * * 博客:http://sunjiajia.com 10 | * * * * Github:https://github.com/opengit 11 | * * * * 12 | * * * * 注意**:如果您使用或者修改该代码,请务必保留此版权信息。 13 | * * * * =================================== 14 | * * * 15 | * * * 16 | * * 17 | * 18 | */ 19 | 20 | package com.sunjiajia.monkeyandroid.mustlearn.databinding.bean; 21 | 22 | import android.databinding.BaseObservable; 23 | import android.databinding.Bindable; 24 | import com.android.databinding.library.baseAdapters.BR; 25 | 26 | /** 27 | * Created by monkey on 16-7-14. 28 | */ 29 | public class User extends BaseObservable { 30 | private String username; 31 | private String password; 32 | private int age; 33 | private String email; 34 | private boolean vip; 35 | 36 | public User(String username, String password, int age, String email, boolean vip) { 37 | this.username = username; 38 | this.password = password; 39 | this.age = age; 40 | this.email = email; 41 | this.vip = vip; 42 | } 43 | 44 | @Bindable public String getUsername() { 45 | return username; 46 | } 47 | 48 | public void setUsername(String username) { 49 | this.username = username; 50 | notifyPropertyChanged(BR.username); 51 | } 52 | 53 | @Bindable public String getPassword() { 54 | return password; 55 | } 56 | 57 | public void setPassword(String password) { 58 | this.password = password; 59 | notifyPropertyChanged(BR.password); 60 | } 61 | 62 | @Bindable public int getAge() { 63 | return age; 64 | } 65 | 66 | public void setAge(int age) { 67 | this.age = age; 68 | notifyPropertyChanged(BR.age); 69 | } 70 | 71 | @Bindable public String getEmail() { 72 | return email; 73 | } 74 | 75 | public void setEmail(String email) { 76 | this.email = email; 77 | notifyPropertyChanged(BR.email); 78 | } 79 | 80 | @Bindable public boolean isVip() { 81 | return vip; 82 | } 83 | 84 | public void setVip(boolean vip) { 85 | this.vip = vip; 86 | notifyPropertyChanged(BR.vip); 87 | } 88 | 89 | @Override public String toString() { 90 | return "User{" + 91 | "username='" + username + '\'' + 92 | ", password='" + password + '\'' + 93 | ", age=" + age + 94 | ", email='" + email + '\'' + 95 | ", vip=" + vip + 96 | '}'; 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /app/src/main/java/com/sunjiajia/monkeyandroid/mustlearn/recyclerview/BaseRecyclerAdapter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * 4 | * * * 5 | * * * * =================================== 6 | * * * * Copyright (c) 2016. 7 | * * * * 作者:安卓猴 8 | * * * * 微博:@安卓猴 9 | * * * * 博客:http://sunjiajia.com 10 | * * * * Github:https://github.com/opengit 11 | * * * * 12 | * * * * 注意**:如果您使用或者修改该代码,请务必保留此版权信息。 13 | * * * * =================================== 14 | * * * 15 | * * * 16 | * * 17 | * 18 | */ 19 | 20 | package com.sunjiajia.monkeyandroid.mustlearn.recyclerview; 21 | 22 | import android.support.v7.widget.RecyclerView; 23 | import android.view.LayoutInflater; 24 | import android.view.View; 25 | import android.view.ViewGroup; 26 | import java.util.ArrayList; 27 | 28 | /** 29 | * Created by monkey on 16-7-1. 30 | */ 31 | public abstract class BaseRecyclerAdapter extends RecyclerView.Adapter { 32 | 33 | private ArrayList list; 34 | 35 | public BaseRecyclerAdapter(ArrayList list) { 36 | this.list = list; 37 | } 38 | 39 | @Override public BaseRecyclerHolder onCreateViewHolder(ViewGroup parent, int viewType) { 40 | 41 | View itemView = 42 | LayoutInflater.from(parent.getContext()).inflate(giveItemLayoutResId(), parent, false); 43 | 44 | return getRecyclerHolder(itemView); 45 | } 46 | 47 | @Override public void onBindViewHolder(BaseRecyclerHolder holder, int position) { 48 | holder.setData(list.get(position)); 49 | } 50 | 51 | @Override public int getItemCount() { 52 | return list.size(); 53 | } 54 | 55 | /** 56 | * 将初始化Holder的任务交给子类 57 | * @param itemView 58 | * 单个itemView,只要实现了giveItemLayoutResId方法,此itemView会自动产生 59 | * @return 60 | * 返回的是一个封装好的继承自BaseRecyclerHolder的ViewHolder 61 | */ 62 | public abstract BaseRecyclerHolder getRecyclerHolder(View itemView); 63 | 64 | /** 65 | *给出Item的布局文件Id,从而产生itemView 66 | * @return 67 | * Item的布局文件Id 68 | */ 69 | public abstract int giveItemLayoutResId(); 70 | } 71 | -------------------------------------------------------------------------------- /app/src/main/java/com/sunjiajia/monkeyandroid/mustlearn/recyclerview/BaseRecyclerHolder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * 4 | * * * 5 | * * * * =================================== 6 | * * * * Copyright (c) 2016. 7 | * * * * 作者:安卓猴 8 | * * * * 微博:@安卓猴 9 | * * * * 博客:http://sunjiajia.com 10 | * * * * Github:https://github.com/opengit 11 | * * * * 12 | * * * * 注意**:如果您使用或者修改该代码,请务必保留此版权信息。 13 | * * * * =================================== 14 | * * * 15 | * * * 16 | * * 17 | * 18 | */ 19 | 20 | package com.sunjiajia.monkeyandroid.mustlearn.recyclerview; 21 | 22 | import android.support.v7.widget.RecyclerView; 23 | import android.view.View; 24 | 25 | /** 26 | * Created by monkey on 16-7-1. 27 | */ 28 | public abstract class BaseRecyclerHolder extends RecyclerView.ViewHolder { 29 | 30 | private View itemView; 31 | private T data; 32 | 33 | public BaseRecyclerHolder(View itemView) { 34 | super(itemView); 35 | this.itemView = itemView; 36 | } 37 | 38 | /** 39 | * 设置数据 40 | * @param data 41 | * 单个数据 42 | */ 43 | public void setData(T data) { 44 | this.data = data; 45 | findAndRefreshItemView(itemView, data); 46 | } 47 | 48 | /** 49 | * 得到单个数据 50 | * @return 51 | * 单个数据 52 | */ 53 | public T getData() { 54 | return data; 55 | } 56 | 57 | /** 58 | * findViewById每个itemView中的控件,并刷新(设置数据、配置)它们 59 | * @param itemView 60 | * 每个item的根视图 61 | * @param data 62 | * 需要设置给itemView和它里面控件的单个数据 63 | */ 64 | public abstract void findAndRefreshItemView(View itemView, T data); 65 | } 66 | -------------------------------------------------------------------------------- /app/src/main/java/com/sunjiajia/monkeyandroid/mustlearn/recyclerview/MyRecyclerAdapter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * 4 | * * * 5 | * * * * =================================== 6 | * * * * Copyright (c) 2016. 7 | * * * * 作者:安卓猴 8 | * * * * 微博:@安卓猴 9 | * * * * 博客:http://sunjiajia.com 10 | * * * * Github:https://github.com/opengit 11 | * * * * 12 | * * * * 注意**:如果您使用或者修改该代码,请务必保留此版权信息。 13 | * * * * =================================== 14 | * * * 15 | * * * 16 | * * 17 | * 18 | */ 19 | 20 | package com.sunjiajia.monkeyandroid.mustlearn.recyclerview; 21 | 22 | import android.view.View; 23 | import com.sunjiajia.monkeyandroid.R; 24 | import java.util.ArrayList; 25 | 26 | /** 27 | * Created by monkey on 16-7-1. 28 | */ 29 | public class MyRecyclerAdapter extends BaseRecyclerAdapter { 30 | 31 | public MyRecyclerAdapter(ArrayList list) { 32 | super(list); 33 | } 34 | 35 | @Override public BaseRecyclerHolder getRecyclerHolder(View itemView) { 36 | return new MyRecyclerHolder(itemView); 37 | } 38 | 39 | @Override public int giveItemLayoutResId() { 40 | return R.layout.item_recycler; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /app/src/main/java/com/sunjiajia/monkeyandroid/mustlearn/recyclerview/MyRecyclerHolder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * 4 | * * * 5 | * * * * =================================== 6 | * * * * Copyright (c) 2016. 7 | * * * * 作者:安卓猴 8 | * * * * 微博:@安卓猴 9 | * * * * 博客:http://sunjiajia.com 10 | * * * * Github:https://github.com/opengit 11 | * * * * 12 | * * * * 注意**:如果您使用或者修改该代码,请务必保留此版权信息。 13 | * * * * =================================== 14 | * * * 15 | * * * 16 | * * 17 | * 18 | */ 19 | 20 | package com.sunjiajia.monkeyandroid.mustlearn.recyclerview; 21 | 22 | import android.view.View; 23 | import android.widget.TextView; 24 | import android.widget.Toast; 25 | import com.sunjiajia.monkeyandroid.R; 26 | 27 | /** 28 | * Created by monkey on 16-7-1. 29 | */ 30 | public class MyRecyclerHolder extends BaseRecyclerHolder { 31 | 32 | public MyRecyclerHolder(View itemView) { 33 | super(itemView); 34 | } 35 | 36 | @Override public void findAndRefreshItemView(final View itemView, final String data) { 37 | TextView textView = (TextView) itemView.findViewById(R.id.item_textview); 38 | textView.setText(data); 39 | 40 | itemView.setOnClickListener(new View.OnClickListener() { 41 | @Override public void onClick(View view) { 42 | Toast.makeText(itemView.getContext(), data + " clicked", Toast.LENGTH_SHORT).show(); 43 | } 44 | }); 45 | 46 | itemView.setOnLongClickListener(new View.OnLongClickListener() { 47 | @Override public boolean onLongClick(View view) { 48 | Toast.makeText(itemView.getContext(), data + " long clicked", Toast.LENGTH_SHORT).show(); 49 | return true; 50 | } 51 | }); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /app/src/main/java/com/sunjiajia/monkeyandroid/mustlearn/recyclerview/SuperRecyclerActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * 4 | * * * 5 | * * * * =================================== 6 | * * * * Copyright (c) 2016. 7 | * * * * 作者:安卓猴 8 | * * * * 微博:@安卓猴 9 | * * * * 博客:http://sunjiajia.com 10 | * * * * Github:https://github.com/opengit 11 | * * * * 12 | * * * * 注意**:如果您使用或者修改该代码,请务必保留此版权信息。 13 | * * * * =================================== 14 | * * * 15 | * * * 16 | * * 17 | * 18 | */ 19 | 20 | package com.sunjiajia.monkeyandroid.mustlearn.recyclerview; 21 | 22 | import android.os.Bundle; 23 | import android.support.annotation.Nullable; 24 | import android.support.v7.widget.LinearLayoutManager; 25 | import android.support.v7.widget.RecyclerView; 26 | import com.sunjiajia.monkeyandroid.R; 27 | import com.sunjiajia.monkeyandroid.baselearn.activity.BaseActivity; 28 | import java.util.ArrayList; 29 | 30 | /** 31 | * Created by monkey on 16-6-30. 32 | */ 33 | public class SuperRecyclerActivity extends BaseActivity { 34 | 35 | private RecyclerView mRecyclerView; 36 | private ArrayList mList; 37 | 38 | @Override public int giveViewResId() { 39 | return R.layout.activity_super_recycler; 40 | } 41 | 42 | @Override protected void onCreate(@Nullable Bundle savedInstanceState) { 43 | super.onCreate(savedInstanceState); 44 | initView(); 45 | configView(); 46 | } 47 | 48 | private void configView() { 49 | mList = new ArrayList<>(); 50 | for (int i = 0; i < 20; i++) { 51 | mList.add("Jack " + i); 52 | } 53 | 54 | LinearLayoutManager manager = 55 | new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false); 56 | mRecyclerView.setLayoutManager(manager); 57 | mRecyclerView.setAdapter(new MyRecyclerAdapter(mList)); 58 | } 59 | 60 | private void initView() { 61 | mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /app/src/main/java/com/sunjiajia/monkeyandroid/mustlearn/sqlite/MySQLiteOpenHelper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * 4 | * * * 5 | * * * * =================================== 6 | * * * * Copyright (c) 2016. 7 | * * * * 作者:安卓猴 8 | * * * * 微博:@安卓猴 9 | * * * * 博客:http://sunjiajia.com 10 | * * * * Github:https://github.com/opengit 11 | * * * * 12 | * * * * 注意**:如果您使用或者修改该代码,请务必保留此版权信息。 13 | * * * * =================================== 14 | * * * 15 | * * * 16 | * * 17 | * 18 | */ 19 | 20 | package com.sunjiajia.monkeyandroid.mustlearn.sqlite; 21 | 22 | import android.content.Context; 23 | import android.database.sqlite.SQLiteDatabase; 24 | import android.database.sqlite.SQLiteOpenHelper; 25 | import android.widget.Toast; 26 | 27 | /** 28 | * Created by monkey on 16-4-26. 29 | * 30 | * SQLite的打开帮助类 31 | */ 32 | public class MySQLiteOpenHelper extends SQLiteOpenHelper { 33 | 34 | private Context mContext; 35 | 36 | /** 37 | * 构造方法 38 | * 39 | * @param context 40 | * 上下文 41 | * @param name 42 | * 数据库文件的名字 43 | * @param factory 44 | * 游标工厂 45 | * @param version 46 | * 数据库的初始版本 47 | */ 48 | public MySQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, 49 | int version) { 50 | super(context, name, factory, version); 51 | this.mContext = context; 52 | } 53 | 54 | /** 55 | * 数据库创建时调用 56 | * @param db 57 | */ 58 | @Override public void onCreate(SQLiteDatabase db) { 59 | Toast.makeText(mContext, "--->数据库被【创建】了---", Toast.LENGTH_SHORT).show(); 60 | 61 | //创建表 62 | String sql = 63 | "create table person(_id integer primary key autoincrement,name char(10),salary char(20),phone integer(20))"; 64 | db.execSQL(sql); 65 | } 66 | 67 | /** 68 | * 数据库的升级 69 | * @param db 70 | * @param oldVersion 71 | * @param newVersion 72 | */ 73 | @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 74 | Toast.makeText(mContext, "--->数据库被【升级】了---", Toast.LENGTH_SHORT).show(); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /app/src/main/java/com/sunjiajia/monkeyandroid/mustlearn/sqlite/SqliteActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * 4 | * * * 5 | * * * * =================================== 6 | * * * * Copyright (c) 2016. 7 | * * * * 作者:安卓猴 8 | * * * * 微博:@安卓猴 9 | * * * * 博客:http://sunjiajia.com 10 | * * * * Github:https://github.com/opengit 11 | * * * * 12 | * * * * 注意**:如果您使用或者修改该代码,请务必保留此版权信息。 13 | * * * * =================================== 14 | * * * 15 | * * * 16 | * * 17 | * 18 | */ 19 | 20 | package com.sunjiajia.monkeyandroid.mustlearn.sqlite; 21 | 22 | import android.content.ContentValues; 23 | import android.database.Cursor; 24 | import android.database.sqlite.SQLiteDatabase; 25 | import android.os.Bundle; 26 | import android.support.annotation.Nullable; 27 | import android.support.v7.app.AppCompatActivity; 28 | import android.widget.ListView; 29 | import butterknife.Bind; 30 | import butterknife.ButterKnife; 31 | import com.sunjiajia.monkeyandroid.R; 32 | import java.util.ArrayList; 33 | import java.util.List; 34 | 35 | /** 36 | * Created by monkey on 16-4-27. 37 | * 38 | * 用来显示从数据库中读取出来读的数据,必须在MustLearnPointActivity中, 39 | * 多次点击“SQLite数据库的使用方法”,来使数据库中有更多的数据,显示出来才明显 40 | */ 41 | public class SqliteActivity extends AppCompatActivity { 42 | @Bind(R.id.listview_sqlite) ListView mListviewSqlite; 43 | 44 | @Override protected void onCreate(@Nullable Bundle savedInstanceState) { 45 | super.onCreate(savedInstanceState); 46 | setContentView(R.layout.activity_sqlite); 47 | ButterKnife.bind(this); 48 | 49 | // 创建数据库 50 | MySQLiteOpenHelper helper = new MySQLiteOpenHelper(this, "students.db", null, 1); 51 | SQLiteDatabase db = helper.getWritableDatabase(); 52 | 53 | initDbData(db); 54 | List dbData = getDbData(db); 55 | mListviewSqlite.setAdapter(new SqliteListViewAdapter(this, dbData)); 56 | } 57 | 58 | private List getDbData(SQLiteDatabase db) { 59 | List dataList = new ArrayList<>(); 60 | Cursor cursor = db.query("person", null, null, null, null, null, null, null); 61 | while (cursor.moveToNext()) { 62 | String name = cursor.getString(cursor.getColumnIndex("name")); 63 | String salary = cursor.getString(cursor.getColumnIndex("salary")); 64 | String phone = cursor.getString(cursor.getColumnIndex("phone")); 65 | dataList.add("name->" + name + ",salary->" + salary + ",phone->" + phone); 66 | } 67 | return dataList; 68 | } 69 | 70 | private void initDbData(SQLiteDatabase db) { 71 | insert(db); 72 | } 73 | 74 | private void insert(SQLiteDatabase db) { 75 | ContentValues values = new ContentValues(); 76 | for (int i = 1; i <= 100; i++) { 77 | values.put("name", "小豆" + i); 78 | values.put("phone", "1380000X" + i); 79 | values.put("salary", i + "000"); 80 | db.insert("person", null, values); 81 | } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /app/src/main/java/com/sunjiajia/monkeyandroid/mustlearn/sqlite/SqliteListViewAdapter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * 4 | * * * 5 | * * * * =================================== 6 | * * * * Copyright (c) 2016. 7 | * * * * 作者:安卓猴 8 | * * * * 微博:@安卓猴 9 | * * * * 博客:http://sunjiajia.com 10 | * * * * Github:https://github.com/opengit 11 | * * * * 12 | * * * * 注意**:如果您使用或者修改该代码,请务必保留此版权信息。 13 | * * * * =================================== 14 | * * * 15 | * * * 16 | * * 17 | * 18 | */ 19 | 20 | package com.sunjiajia.monkeyandroid.mustlearn.sqlite; 21 | 22 | import android.content.Context; 23 | import android.view.LayoutInflater; 24 | import android.view.View; 25 | import android.view.ViewGroup; 26 | import android.widget.BaseAdapter; 27 | import android.widget.TextView; 28 | import butterknife.Bind; 29 | import butterknife.ButterKnife; 30 | import com.sunjiajia.monkeyandroid.R; 31 | import java.util.List; 32 | 33 | /** 34 | * Created by monkey on 16-4-27. 35 | */ 36 | public class SqliteListViewAdapter extends BaseAdapter { 37 | 38 | private Context mContext; 39 | private List mStringList; 40 | private LayoutInflater mInflater; 41 | 42 | public SqliteListViewAdapter(Context context, List list) { 43 | mInflater = LayoutInflater.from(context); 44 | this.mContext = context; 45 | this.mStringList = list; 46 | } 47 | 48 | @Override public int getCount() { 49 | return mStringList.size(); 50 | } 51 | 52 | @Override public Object getItem(int position) { 53 | return mStringList.get(position); 54 | } 55 | 56 | @Override public long getItemId(int position) { 57 | return position; 58 | } 59 | 60 | @Override public View getView(int position, View convertView, ViewGroup parent) { 61 | 62 | MyViewHolder holder; 63 | 64 | if (convertView == null) { 65 | convertView = mInflater.inflate(R.layout.item_sqlite, parent, false); 66 | holder = new MyViewHolder(convertView); 67 | convertView.setTag(holder); 68 | } else { 69 | holder = (MyViewHolder) convertView.getTag(); 70 | } 71 | 72 | holder.mTvItem.setText(mStringList.get(position)); 73 | 74 | return convertView; 75 | } 76 | 77 | static class MyViewHolder { 78 | @Bind(R.id.tv_item) TextView mTvItem; 79 | 80 | MyViewHolder(View view) { 81 | ButterKnife.bind(this, view); 82 | } 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /app/src/main/java/com/sunjiajia/monkeyandroid/test/UnitTestDemo.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * 4 | * * * =================================== 5 | * * * Copyright (c) 2016. 6 | * * * 作者:安卓猴 7 | * * * 微博:@安卓猴 8 | * * * 博客:http://sunjiajia.com 9 | * * * Github:https://github.com/opengit 10 | * * * 11 | * * * 注意**:如果您使用或者修改该代码,请务必保留此版权信息。 12 | * * * =================================== 13 | * * 14 | * * 15 | * 16 | */ 17 | 18 | package com.sunjiajia.monkeyandroid.test; 19 | 20 | import android.test.AndroidTestCase; 21 | 22 | /** 23 | * Created by Administrator on 2016/4/20. 24 | */ 25 | public class UnitTestDemo extends AndroidTestCase { 26 | public void test() { 27 | System.out.println("UnitTestDemo-->test--> is running"); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /app/src/main/java/com/sunjiajia/monkeyandroid/utils/PermissionsUtil.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * 4 | * * * =================================== 5 | * * * Copyright (c) 2016. 6 | * * * 作者:安卓猴 7 | * * * 微博:@安卓猴 8 | * * * 博客:http://sunjiajia.com 9 | * * * Github:https://github.com/opengit 10 | * * * 11 | * * * 注意**:如果您使用或者修改该代码,请务必保留此版权信息。 12 | * * * =================================== 13 | * * 14 | * * 15 | * 16 | */ 17 | 18 | package com.sunjiajia.monkeyandroid.utils; 19 | 20 | import android.Manifest; 21 | import android.app.Activity; 22 | import android.content.Context; 23 | import android.content.Intent; 24 | import android.content.SharedPreferences; 25 | import android.content.pm.PackageManager; 26 | import android.net.Uri; 27 | import android.provider.Settings; 28 | import android.support.design.widget.Snackbar; 29 | import android.support.v4.app.ActivityCompat; 30 | import android.view.View; 31 | import com.sunjiajia.monkeyandroid.R; 32 | import java.util.ArrayList; 33 | 34 | /** 35 | * 权限控制工具类: 36 | * 为了适配API23,即Android M 在清单文件中配置use permissions后,还要在程序运行的时候进行申请。 37 | * 38 | * ***整个权限的申请与处理的过程是这样的: 39 | * *****1.进入主Activity,首先申请所有的权限; 40 | * *****2.用户对权限进行授权,有2种情况: 41 | * ********1).用户Allow了权限,则表示该权限已经被授权,无须其它操作; 42 | * ********2).用户Deny了权限,则下次启动Activity会再次弹出系统的Permisssions申请授权对话框。 43 | * *****3.如果用户Deny了权限,那么下次再次进入Activity,会再次申请权限,这次的权限对话框上,会有一个选项“never ask again”: 44 | * ********1).如果用户勾选了“never ask again”的checkbox,下次启动时就必须自己写Dialog或者Snackbar引导用户到应用设置里面去手动授予权限; 45 | * ********2).如果用户未勾选上面的选项,若选择了Allow,则表示该权限已经被授权,无须其它操作; 46 | * ********3).如果用户未勾选上面的选项,若选择了Deny,则下次启动Activity会再次弹出系统的Permisssions申请授权对话框。 47 | */ 48 | public class PermissionsUtil { 49 | 50 | // 状态码、标志位 51 | private static final int REQUEST_STATUS_CODE = 0x001; 52 | private static final int REQUEST_PERMISSION_SETTING = 0x002; 53 | 54 | //常量字符串数组,将需要申请的权限写进去,同时必须要在Androidmanifest.xml中声明。 55 | private static String[] PERMISSIONS_GROUP = { 56 | Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, 57 | Manifest.permission.INTERNET, Manifest.permission.CAMERA, 58 | Manifest.permission.ACCESS_NETWORK_STATE, Manifest.permission.ACCESS_WIFI_STATE, 59 | Manifest.permission.RECORD_AUDIO, Manifest.permission.WRITE_CONTACTS, 60 | Manifest.permission.SEND_SMS, Manifest.permission.CALL_PHONE 61 | }; 62 | 63 | public static void checkAndRequestPermissions(final Activity activity) { 64 | 65 | // 一个list,用来存放没有被授权的权限 66 | ArrayList denidArray = new ArrayList<>(); 67 | 68 | // 遍历PERMISSIONS_GROUP,将没有被授权的权限存放进denidArray 69 | for (String permission : PERMISSIONS_GROUP) { 70 | int grantCode = ActivityCompat.checkSelfPermission(activity, permission); 71 | if (grantCode == PackageManager.PERMISSION_DENIED) { 72 | denidArray.add(permission); 73 | } 74 | } 75 | 76 | // 将denidArray转化为字符串数组,方便下面调用requestPermissions来请求授权 77 | String[] denidPermissions = denidArray.toArray(new String[denidArray.size()]); 78 | 79 | // 如果该字符串数组长度大于0,说明有未被授权的权限 80 | if (denidPermissions.length > 0) { 81 | // 遍历denidArray,用showRationaleUI来判断,每一个没有得到授权的权限是否是用户手动拒绝的 82 | for (String permission : denidArray) { 83 | // 如果permission是用户手动拒绝的,则用SnackBar来引导用户进入App设置页面,手动授予权限 84 | if (!showRationaleUI(activity, permission)) { 85 | // 判断App是否是首次启动 86 | if (!isAppFirstRun(activity)) { 87 | Snackbar snackbar = 88 | Snackbar.make(activity.findViewById(R.id.btn_sdcard_avail), "这次是真的需要去授权了", 89 | Snackbar.LENGTH_LONG); 90 | snackbar.setAction("前往设置", new View.OnClickListener() { 91 | @Override public void onClick(View v) { 92 | // 进入App设置页面 93 | Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); 94 | Uri uri = Uri.fromParts("package", activity.getPackageName(), null); 95 | intent.setData(uri); 96 | activity.startActivityForResult(intent, REQUEST_PERMISSION_SETTING); 97 | } 98 | }); 99 | snackbar.show(); 100 | } 101 | } 102 | break; 103 | } 104 | requestPermissions(activity, denidPermissions); 105 | } 106 | } 107 | 108 | /** 109 | * 关于shouldShowRequestPermissionRationale函数的一点儿注意事项: 110 | * ***1).应用安装后第一次访问,则直接返回false; 111 | * ***2).第一次请求权限时,用户Deny了,再次调用shouldShowRequestPermissionRationale(),则返回true; 112 | * ***3).第二次请求权限时,用户Deny了,并选择了“never ask again”的选项时,再次调用shouldShowRequestPermissionRationale()时,返回false; 113 | * ***4).设备的系统设置中,禁止了应用获取这个权限的授权,则调用shouldShowRequestPermissionRationale(),返回false。 114 | */ 115 | public static boolean showRationaleUI(Activity activity, String permission) { 116 | return ActivityCompat.shouldShowRequestPermissionRationale(activity, permission); 117 | } 118 | 119 | /** 120 | * 对权限字符串数组中的所有权限进行申请授权,如果用户选择了“never ask again”,则不会弹出系统的Permission申请授权对话框 121 | */ 122 | public static void requestPermissions(Activity activity, String[] permissions) { 123 | ActivityCompat.requestPermissions(activity, permissions, REQUEST_STATUS_CODE); 124 | } 125 | 126 | /** 127 | * 用来判断,App是否是首次启动: 128 | * ***由于每次调用shouldShowRequestPermissionRationale得到的结果因情况而变,因此必须判断一下App是否首次启动,才能控制好出现Dialog和SnackBar的时机 129 | */ 130 | public static boolean isAppFirstRun(Activity activity) { 131 | SharedPreferences sp = activity.getSharedPreferences("config", Context.MODE_PRIVATE); 132 | SharedPreferences.Editor editor = sp.edit(); 133 | 134 | if (sp.getBoolean("first_run", true)) { 135 | editor.putBoolean("first_run", false); 136 | editor.commit(); 137 | return true; 138 | } else { 139 | editor.putBoolean("first_run", false); 140 | editor.commit(); 141 | return false; 142 | } 143 | } 144 | } 145 | -------------------------------------------------------------------------------- /app/src/main/java/com/sunjiajia/monkeyandroid/utils/Utils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * 4 | * * * 5 | * * * * =================================== 6 | * * * * Copyright (c) 2016. 7 | * * * * 作者:安卓猴 8 | * * * * 微博:@安卓猴 9 | * * * * 博客:http://sunjiajia.com 10 | * * * * Github:https://github.com/opengit 11 | * * * * 12 | * * * * 注意**:如果您使用或者修改该代码,请务必保留此版权信息。 13 | * * * * =================================== 14 | * * * 15 | * * * 16 | * * 17 | * 18 | */ 19 | 20 | package com.sunjiajia.monkeyandroid.utils; 21 | 22 | import java.io.ByteArrayOutputStream; 23 | import java.io.InputStream; 24 | 25 | /** 26 | * Created by monkey on 16-4-29. 27 | * 28 | * 工具类 29 | */ 30 | public class Utils { 31 | /** 32 | * 从输入流中读取数据为文本 33 | * @param inputStream 34 | * 输入流 35 | * @return 36 | * 文本 37 | */ 38 | public static String getTextFromStream(InputStream inputStream) { 39 | byte[] bytes = new byte[1024]; 40 | int len = 0; 41 | // 创建字节数组输出流,读取输入流的文本数据时,同步把数据写入数组输入流 42 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); 43 | try { 44 | while ((len = inputStream.read(bytes)) != -1) { 45 | baos.write(bytes, 0, len); 46 | } 47 | return new String(baos.toByteArray()); 48 | } catch (Exception e) { 49 | e.printStackTrace(); 50 | } 51 | return null; 52 | } 53 | 54 | /** 55 | * 从文件网址链接中获取文件的名称(包括扩展名) 56 | * @param url 57 | * 网址链接 58 | * @return 59 | * 文件的名称(包括扩展名) 60 | */ 61 | public static String getFileNameFromUrl(String url) { 62 | return url.substring(url.lastIndexOf("/") + 1); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/bg_btn.xml: -------------------------------------------------------------------------------- 1 | 2 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/monkey.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/opengit/MonkeyAndroid/bbb0eb5941496ae6f908ecb0427b075b5506ed0b/app/src/main/res/drawable/monkey.jpg -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_bcr.xml: -------------------------------------------------------------------------------- 1 | 2 | 20 | 21 | 29 | 30 |