├── app ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ ├── values │ │ │ │ ├── strings.xml │ │ │ │ ├── colors.xml │ │ │ │ └── styles.xml │ │ │ ├── mipmap-hdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-mdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xxhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xxxhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-anydpi-v26 │ │ │ │ ├── ic_launcher.xml │ │ │ │ └── ic_launcher_round.xml │ │ │ ├── layout │ │ │ │ └── activity_main.xml │ │ │ ├── drawable-v24 │ │ │ │ └── ic_launcher_foreground.xml │ │ │ └── drawable │ │ │ │ └── ic_launcher_background.xml │ │ ├── aidl │ │ │ └── com │ │ │ │ └── android │ │ │ │ └── internal │ │ │ │ └── telephony │ │ │ │ └── ITelephony.aidl │ │ ├── java │ │ │ └── com │ │ │ │ └── example │ │ │ │ └── yuanting │ │ │ │ └── msgpushandcall │ │ │ │ ├── service │ │ │ │ ├── IComeMessage.java │ │ │ │ ├── ComeMessage.java │ │ │ │ └── NotifyService.java │ │ │ │ ├── utils │ │ │ │ └── PhoneCallUtil.java │ │ │ │ └── MainActivity.java │ │ └── AndroidManifest.xml │ ├── test │ │ └── java │ │ │ └── com │ │ │ └── example │ │ │ └── yuanting │ │ │ └── msgpushandcall │ │ │ └── ExampleUnitTest.java │ └── androidTest │ │ └── java │ │ └── com │ │ └── example │ │ └── yuanting │ │ └── msgpushandcall │ │ └── ExampleInstrumentedTest.java ├── proguard-rules.pro ├── build.gradle └── app.iml └── .idea ├── vcs.xml ├── libraries ├── android_android_26.xml ├── net_sf_kxml_kxml2_2_3_0_jar.xml ├── com_google_code_findbugs_jsr305_2_0_1_jar.xml ├── com_android_support_constraint_constraint_layout_solver_1_1_2_jar.xml ├── junit_junit_4_12_jar.xml ├── com_android_support_constraint_constraint_layout_1_1_2.xml ├── javax_inject_javax_inject_1_jar.xml ├── android_arch_core_common_1_0_0_jar.xml ├── com_squareup_javawriter_2_1_1_jar.xml ├── org_hamcrest_hamcrest_core_1_3_jar.xml ├── android_arch_lifecycle_common_1_0_0_jar.xml ├── org_hamcrest_hamcrest_library_1_3_jar.xml ├── org_hamcrest_hamcrest_integration_1_3_jar.xml ├── com_android_support_support_annotations_26_1_0_jar.xml ├── com_android_support_support_annotations_27_1_1_jar.xml ├── android_arch_lifecycle_runtime_1_0_0.xml ├── com_android_support_test_runner_1_0_2.xml ├── com_android_support_test_monitor_1_0_2.xml ├── com_android_support_support_v4_26_1_0.xml ├── com_android_support_appcompat_v7_26_1_0.xml ├── com_android_support_support_compat_26_1_0.xml ├── com_android_support_support_core_ui_26_1_0.xml ├── com_android_support_support_fragment_26_1_0.xml ├── com_android_support_support_core_utils_26_1_0.xml ├── com_android_support_test_espresso_espresso_core_3_0_2.xml ├── com_android_support_support_media_compat_26_1_0.xml ├── com_android_support_support_vector_drawable_26_1_0.xml ├── com_android_support_animated_vector_drawable_26_1_0.xml └── com_android_support_test_espresso_espresso_idling_resource_3_0_2.xml └── modules.xml /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | MsgPushAndCall 3 | 4 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodoonDemo/MsgPushAndCallDemo/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodoonDemo/MsgPushAndCallDemo/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodoonDemo/MsgPushAndCallDemo/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodoonDemo/MsgPushAndCallDemo/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodoonDemo/MsgPushAndCallDemo/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodoonDemo/MsgPushAndCallDemo/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodoonDemo/MsgPushAndCallDemo/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodoonDemo/MsgPushAndCallDemo/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodoonDemo/MsgPushAndCallDemo/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodoonDemo/MsgPushAndCallDemo/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/aidl/com/android/internal/telephony/ITelephony.aidl: -------------------------------------------------------------------------------- 1 | // ITelephony.aidl 2 | package com.android.internal.telephony; 3 | 4 | // Declare any non-default types here with import statements 5 | 6 | interface ITelephony { 7 | 8 | boolean endCall(); 9 | 10 | } 11 | -------------------------------------------------------------------------------- /.idea/libraries/android_android_26.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /.idea/libraries/net_sf_kxml_kxml2_2_3_0_jar.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/libraries/com_google_code_findbugs_jsr305_2_0_1_jar.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/libraries/com_android_support_constraint_constraint_layout_solver_1_1_2_jar.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/yuanting/msgpushandcall/service/IComeMessage.java: -------------------------------------------------------------------------------- 1 | package com.example.yuanting.msgpushandcall.service; 2 | 3 | /** 4 | * Created by yuanting on 2018/8/10. 5 | */ 6 | 7 | public interface IComeMessage { 8 | 9 | /** 10 | * 短信来了 11 | */ 12 | void comeShortMessage(String msg); 13 | 14 | /** 15 | * 微信消息 16 | */ 17 | void comeWxMessage(String msg); 18 | 19 | /** 20 | * qq消息 21 | */ 22 | void comeQQmessage(String msg); 23 | 24 | } 25 | -------------------------------------------------------------------------------- /.idea/libraries/junit_junit_4_12_jar.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /app/src/test/java/com/example/yuanting/msgpushandcall/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.example.yuanting.msgpushandcall; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() throws Exception { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /.idea/libraries/com_android_support_constraint_constraint_layout_1_1_2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.idea/libraries/javax_inject_javax_inject_1_jar.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /.idea/libraries/android_arch_core_common_1_0_0_jar.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /.idea/libraries/com_squareup_javawriter_2_1_1_jar.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /.idea/libraries/org_hamcrest_hamcrest_core_1_3_jar.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /.idea/libraries/android_arch_lifecycle_common_1_0_0_jar.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /.idea/libraries/org_hamcrest_hamcrest_library_1_3_jar.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /.idea/libraries/org_hamcrest_hamcrest_integration_1_3_jar.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /.idea/libraries/com_android_support_support_annotations_26_1_0_jar.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /.idea/libraries/com_android_support_support_annotations_27_1_1_jar.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /.idea/libraries/android_arch_lifecycle_runtime_1_0_0.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/libraries/com_android_support_test_runner_1_0_2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/libraries/com_android_support_test_monitor_1_0_2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/libraries/com_android_support_support_v4_26_1_0.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/libraries/com_android_support_appcompat_v7_26_1_0.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/libraries/com_android_support_support_compat_26_1_0.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/libraries/com_android_support_support_core_ui_26_1_0.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/libraries/com_android_support_support_fragment_26_1_0.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/libraries/com_android_support_support_core_utils_26_1_0.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/libraries/com_android_support_test_espresso_espresso_core_3_0_2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/libraries/com_android_support_support_media_compat_26_1_0.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/libraries/com_android_support_support_vector_drawable_26_1_0.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/libraries/com_android_support_animated_vector_drawable_26_1_0.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /.idea/libraries/com_android_support_test_espresso_espresso_idling_resource_3_0_2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/example/yuanting/msgpushandcall/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.example.yuanting.msgpushandcall; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumented test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.example.yuanting.msgpushandcall", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 26 5 | defaultConfig { 6 | applicationId "com.example.yuanting.msgpushandcall" 7 | minSdkVersion 24 8 | targetSdkVersion 26 9 | versionCode 1 10 | versionName "1.0" 11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | } 20 | 21 | dependencies { 22 | implementation fileTree(dir: 'libs', include: ['*.jar']) 23 | implementation 'com.android.support:appcompat-v7:26.1.0' 24 | implementation 'com.android.support.constraint:constraint-layout:1.1.2' 25 | testImplementation 'junit:junit:4.12' 26 | androidTestImplementation 'com.android.support.test:runner:1.0.2' 27 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' 28 | } 29 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 20 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/yuanting/msgpushandcall/utils/PhoneCallUtil.java: -------------------------------------------------------------------------------- 1 | package com.example.yuanting.msgpushandcall.utils; 2 | 3 | import android.content.Context; 4 | import android.os.RemoteException; 5 | import android.telephony.TelephonyManager; 6 | 7 | import com.android.internal.telephony.ITelephony; 8 | 9 | import java.lang.reflect.InvocationTargetException; 10 | import java.lang.reflect.Method; 11 | 12 | /** 13 | * Created by yuanting on 2018/9/2. 14 | */ 15 | 16 | public class PhoneCallUtil { 17 | 18 | public static void endPhone(Context context) { 19 | TelephonyManager telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); 20 | Method method = null; 21 | try { 22 | method = TelephonyManager.class.getDeclaredMethod("getITelephony"); 23 | method.setAccessible(true); 24 | ITelephony telephony = (ITelephony) method.invoke(telephonyManager); 25 | telephony.endCall(); 26 | } catch (NoSuchMethodException e) { 27 | e.printStackTrace(); 28 | } catch (IllegalAccessException e) { 29 | e.printStackTrace(); 30 | } catch (RemoteException e) { 31 | e.printStackTrace(); 32 | } catch (InvocationTargetException e) { 33 | e.printStackTrace(); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/yuanting/msgpushandcall/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.example.yuanting.msgpushandcall; 2 | 3 | import android.Manifest; 4 | import android.content.Context; 5 | import android.content.pm.PackageManager; 6 | import android.os.Bundle; 7 | import android.support.v4.app.ActivityCompat; 8 | import android.support.v7.app.AppCompatActivity; 9 | import android.telephony.PhoneStateListener; 10 | import android.telephony.TelephonyManager; 11 | import android.util.Log; 12 | import android.view.View; 13 | 14 | import com.example.yuanting.msgpushandcall.service.ComeMessage; 15 | import com.example.yuanting.msgpushandcall.service.IComeMessage; 16 | import com.example.yuanting.msgpushandcall.utils.PhoneCallUtil; 17 | 18 | public class MainActivity extends AppCompatActivity implements IComeMessage{ 19 | 20 | private TelephonyManager telephonyManager; 21 | 22 | private PhoneCallListener callListener; 23 | 24 | @Override 25 | protected void onCreate(Bundle savedInstanceState) { 26 | super.onCreate(savedInstanceState); 27 | setContentView(R.layout.activity_main); 28 | 29 | findViewById(R.id.tv_phone).setOnClickListener(new View.OnClickListener() { 30 | @Override 31 | public void onClick(View v) { 32 | telephonyManager.listen(callListener, PhoneStateListener.LISTEN_CALL_STATE); 33 | } 34 | }); 35 | if(ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED){ 36 | ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.CALL_PHONE},1000); 37 | } 38 | 39 | findViewById(R.id.tv_push_msg).setOnClickListener(new View.OnClickListener() { 40 | @Override 41 | public void onClick(View v) { 42 | ComeMessage comeMessage = new ComeMessage(MainActivity.this,MainActivity.this); 43 | if(!comeMessage.isEnabled()){ 44 | comeMessage.openSetting(); 45 | comeMessage.toggleNotificationListenerService(); 46 | } 47 | } 48 | }); 49 | 50 | telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); 51 | callListener = new PhoneCallListener(); 52 | } 53 | 54 | @Override 55 | public void comeShortMessage(String msg) { 56 | Log.d("comeShortMessage",msg); 57 | } 58 | 59 | @Override 60 | public void comeWxMessage(String msg) { 61 | Log.d("comeWxMessage",msg); 62 | } 63 | 64 | @Override 65 | public void comeQQmessage(String msg) { 66 | Log.d("comeQQmessage",msg); 67 | } 68 | 69 | 70 | /** 71 | * 监听来电状态 72 | */ 73 | public class PhoneCallListener extends PhoneStateListener { 74 | @Override 75 | public void onCallStateChanged(int state, String incomingNumber) { 76 | switch (state) { 77 | case TelephonyManager.CALL_STATE_OFFHOOK: //电话通话的状态 78 | break; 79 | 80 | case TelephonyManager.CALL_STATE_RINGING: //电话响铃的状态 81 | PhoneCallUtil.endPhone(MainActivity.this); 82 | break; 83 | 84 | } 85 | super.onCallStateChanged(state, incomingNumber); 86 | } 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/yuanting/msgpushandcall/service/ComeMessage.java: -------------------------------------------------------------------------------- 1 | package com.example.yuanting.msgpushandcall.service; 2 | 3 | import android.app.Activity; 4 | import android.content.BroadcastReceiver; 5 | import android.content.ComponentName; 6 | import android.content.Context; 7 | import android.content.Intent; 8 | import android.content.IntentFilter; 9 | import android.content.pm.PackageManager; 10 | import android.os.Bundle; 11 | import android.provider.Settings; 12 | import android.text.TextUtils; 13 | 14 | /** 15 | * Created by yuanting on 2018/8/10. 16 | */ 17 | 18 | public class ComeMessage { 19 | private static final String ENABLED_NOTIFICATION_LISTENERS = "enabled_notification_listeners"; 20 | public static final String QQ="com.tencent.mobileqq"; 21 | public static final String WX="com.tencent.mm"; 22 | public static final String MMS="com.android.mms"; 23 | private IComeMessage myMessage; 24 | private Context context; 25 | 26 | public ComeMessage(IComeMessage myMessage, Context context) { 27 | this.myMessage = myMessage; 28 | this.context = context; 29 | registBroadCast(); 30 | } 31 | 32 | private BroadcastReceiver receiver = new BroadcastReceiver() { 33 | @Override 34 | public void onReceive(Context context, Intent intent) { 35 | Bundle bundle=intent.getExtras(); 36 | String packageName = bundle.getString("packageName"); 37 | String msg = bundle.getString("content"); 38 | 39 | if(packageName.contains(WX)){ 40 | myMessage.comeWxMessage(msg); 41 | }else if(packageName.contains(QQ)){ 42 | myMessage.comeQQmessage(msg); 43 | }else if(packageName.contains(MMS)){ 44 | myMessage.comeShortMessage(msg); 45 | } 46 | } 47 | }; 48 | private void registBroadCast() { 49 | IntentFilter filter=new IntentFilter(NotifyService.SEND_MSG_BROADCAST); 50 | context.registerReceiver(receiver,filter); 51 | } 52 | 53 | public void unRegistBroadcast(){ 54 | context.unregisterReceiver(receiver); 55 | } 56 | 57 | public void openSetting(){ 58 | Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"); 59 | if(!(context instanceof Activity)){ 60 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 61 | } 62 | context.startActivity(intent); 63 | } 64 | 65 | public boolean isEnabled() { 66 | String pkgName = context.getPackageName(); 67 | final String flat = Settings.Secure.getString(context.getContentResolver(), 68 | ENABLED_NOTIFICATION_LISTENERS); 69 | if (!TextUtils.isEmpty(flat)) { 70 | final String[] names = flat.split(":"); 71 | for (int i = 0; i < names.length; i++) { 72 | final ComponentName cn = ComponentName.unflattenFromString(names[i]); 73 | if (cn != null) { 74 | if (TextUtils.equals(pkgName, cn.getPackageName())) { 75 | return true; 76 | } 77 | } 78 | } 79 | } 80 | return false; 81 | } 82 | 83 | public void toggleNotificationListenerService() { 84 | PackageManager pm = context.getPackageManager(); 85 | pm.setComponentEnabledSetting( 86 | new ComponentName(context,NotifyService.class), 87 | PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); 88 | 89 | pm.setComponentEnabledSetting( 90 | new ComponentName(context,NotifyService.class), 91 | PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP); 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/yuanting/msgpushandcall/service/NotifyService.java: -------------------------------------------------------------------------------- 1 | package com.example.yuanting.msgpushandcall.service; 2 | 3 | import android.annotation.SuppressLint; 4 | import android.app.Notification; 5 | import android.content.Intent; 6 | import android.os.Bundle; 7 | import android.service.notification.NotificationListenerService; 8 | import android.service.notification.StatusBarNotification; 9 | import android.widget.RemoteViews; 10 | 11 | import java.lang.reflect.Field; 12 | import java.util.ArrayList; 13 | import java.util.HashMap; 14 | import java.util.Map; 15 | 16 | /** 17 | * Created by yuanting on 2018/9/2. 18 | */ 19 | 20 | @SuppressLint("Registered") 21 | public class NotifyService extends NotificationListenerService { 22 | 23 | public static final String SEND_MSG_BROADCAST = "SEND_MSG_BROADCAST"; 24 | 25 | @Override 26 | public void onNotificationPosted(StatusBarNotification sbn) { 27 | 28 | } 29 | 30 | @Override 31 | public void onNotificationRemoved(StatusBarNotification sbn) { 32 | } 33 | 34 | @Override 35 | public void onNotificationPosted(StatusBarNotification sbn, RankingMap rankingMap) { 36 | String packageName = sbn.getPackageName(); 37 | if (!packageName.contains(ComeMessage.MMS) && !packageName.contains(ComeMessage.QQ) && !packageName.contains(ComeMessage.WX)) { 38 | return; 39 | } 40 | Intent intent = new Intent(); 41 | intent.setAction(SEND_MSG_BROADCAST); 42 | Bundle bundle = new Bundle(); 43 | bundle.putString("packageName", packageName); 44 | String content = null; 45 | if (sbn.getNotification().tickerText != null) { 46 | content = sbn.getNotification().tickerText.toString(); 47 | } 48 | if (content == null) { 49 | Map info = getNotiInfo(sbn.getNotification()); 50 | if (info != null) { 51 | content = info.get("title") + ":" + info.get("text"); 52 | } 53 | } 54 | if(content == null || content.length() == 1){ 55 | return; 56 | } 57 | intent.putExtra("content", content); 58 | intent.putExtras(bundle); 59 | this.sendBroadcast(intent); 60 | } 61 | 62 | @Override 63 | public void onNotificationRemoved(StatusBarNotification sbn, RankingMap rankingMap) { 64 | } 65 | 66 | @Override 67 | public StatusBarNotification[] getActiveNotifications() { 68 | return super.getActiveNotifications(); 69 | } 70 | 71 | /** 72 | * 反射取通知栏信息 73 | * 74 | * @param notification 75 | * @return 返回短信内容 76 | */ 77 | private Map getNotiInfo(Notification notification) { 78 | int key = 0; 79 | if (notification == null) 80 | return null; 81 | RemoteViews views = notification.contentView; 82 | if (views == null) 83 | return null; 84 | Class secretClass = views.getClass(); 85 | 86 | try { 87 | Map text = new HashMap<>(); 88 | 89 | Field outerFields[] = secretClass.getDeclaredFields(); 90 | for (int i = 0; i < outerFields.length; i++) { 91 | if (!outerFields[i].getName().equals("mActions")) 92 | continue; 93 | 94 | outerFields[i].setAccessible(true); 95 | 96 | ArrayList actions = (ArrayList) outerFields[i].get(views); 97 | for (Object action : actions) { 98 | Field innerFields[] = action.getClass().getDeclaredFields(); 99 | Object value = null; 100 | Integer type = null; 101 | for (Field field : innerFields) { 102 | field.setAccessible(true); 103 | if (field.getName().equals("value")) { 104 | value = field.get(action); 105 | } else if (field.getName().equals("type")) { 106 | type = field.getInt(action); 107 | } 108 | } 109 | // 经验所得 type 等于9 10为短信title和内容,不排除其他厂商拿不到的情况 110 | if (type != null && (type == 9 || type == 10)) { 111 | if (key == 0) { 112 | text.put("title", value != null ? value.toString() : ""); 113 | } else if (key == 1) { 114 | text.put("text", value != null ? value.toString() : ""); 115 | } else { 116 | text.put(Integer.toString(key), value != null ? value.toString() : null); 117 | } 118 | key++; 119 | } 120 | } 121 | key = 0; 122 | 123 | } 124 | return text; 125 | } catch (Exception e) { 126 | e.printStackTrace(); 127 | } 128 | return null; 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/app.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 8 | 9 | 10 | 11 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 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 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | --------------------------------------------------------------------------------