Browse Source

add 广播接收

liuyuqi-dellpc 1 year ago
parent
commit
65e960efdc

+ 7 - 3
app/src/main/AndroidManifest.xml

@@ -4,15 +4,19 @@
 
     <!-- 电话服务添加 -->
     <uses-permission android:name="android.permission.CALL_PHONE" />
-    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
-    
-    <!-- 开免提 -->
+    <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <!-- 开免提 -->
     <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
+    <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
 
     <application
         android:icon="@drawable/ic_launcher"
         android:label="@string/app_name"
         android:theme="@style/AppTheme">
+        <receiver
+            android:name=".listener.CallReceiver"
+            android:enabled="true"
+          />
+
         <activity
             android:name="com.lyq.autocall.MainActivity"
             android:exported="true">

+ 50 - 66
app/src/main/java/com/lyq/autocall/MainActivity.java

@@ -14,8 +14,6 @@ import android.os.Message;
 import android.telephony.PhoneStateListener;
 import android.telephony.TelephonyManager;
 import android.util.Log;
-import android.view.View;
-import android.view.View.OnClickListener;
 import android.widget.Button;
 import android.widget.EditText;
 import android.widget.TextView;
@@ -60,78 +58,61 @@ public class MainActivity extends Activity {
         //声明一个优先级设置 类型,载入配置文件
         sharedPreferences = getSharedPreferences("AutoCall", MODE_PRIVATE);
         edt1.setText(sharedPreferences.getString("number", ""));
-        btn1.setOnClickListener(new OnClickListener() {
-
-            @Override
-            public void onClick(View v) {
-                if (edt1.getText().toString().trim().length() > 5
-                        && edt2.getText().toString().trim().length() > 0) {
-                    //如果输入的是电话号码,则拨打
-                    number = edt1.getText().toString().trim();
-                    Editor editor = sharedPreferences.edit();
-                    editor.putString("number", number);
-                    editor.apply();
-                    Toast.makeText(getApplicationContext(),
-                            "AutoCall现在带您去挂断重拨", Toast.LENGTH_SHORT).show();
-                    amount = Integer.parseInt(edt2.getText().toString().trim());
-                    end2doCall();
-                } else if (edt1.getText().toString().trim().length() <= 5) {
-                    //不拨打,提示输入号码错误
-                    Toast.makeText(getApplicationContext(),
-                            "检测到输入的非手机号码,傻眼了吧?", Toast.LENGTH_SHORT).show();
-                } else {
-                    Toast.makeText(getApplicationContext(), "亲,至少拨打一次呀!",
-                            Toast.LENGTH_SHORT).show();
-                }
-
+        btn1.setOnClickListener(v -> {
+            if (edt1.getText().toString().trim().length() > 5
+                    && edt2.getText().toString().trim().length() > 0) {
+                //如果输入的是电话号码,则拨打
+                number = edt1.getText().toString().trim();
+                Editor editor = sharedPreferences.edit();
+                editor.putString("number", number);
+                editor.apply();
+                Toast.makeText(getApplicationContext(),
+                        "AutoCall现在带您去挂断重拨", Toast.LENGTH_SHORT).show();
+                amount = Integer.parseInt(edt2.getText().toString().trim());
+                end2doCall();
+            } else if (edt1.getText().toString().trim().length() <= 5) {
+                //不拨打,提示输入号码错误
+                Toast.makeText(getApplicationContext(),
+                        "检测到输入的非手机号码,傻眼了吧?", Toast.LENGTH_SHORT).show();
+            } else {
+                Toast.makeText(getApplicationContext(), "亲,至少拨打一次呀!",
+                        Toast.LENGTH_SHORT).show();
             }
+
         });
-        btn2.setOnClickListener(new OnClickListener() {
-
-            @Override
-            public void onClick(View v) {
-                if (edt1.getText().toString().trim().length() > 5
-                        && edt2.getText().toString().trim().length() > 0) {
-                    //如果输入的是电话号码,则拨打
-                    number = edt1.getText().toString().trim();
-                    Editor editor = sharedPreferences.edit();
-                    editor.putString("number", number);
-                    editor.apply();
-                    amount = Integer.parseInt(edt2.getText().toString().trim());
-                    if (thread == null) {
-                        Toast.makeText(getApplicationContext(),
-                                "AutoCall现在带您去自动重拨", Toast.LENGTH_SHORT).show();
-                        thread = new Thread(new Runnable() {
-                            @Override
-                            public void run() {
-                                handler.sendEmptyMessage(0);
-                            }
-                        });
-                        thread.start();
-                        Log.i(tag, "thread started");
-                    } else {
-                        Toast.makeText(getApplicationContext(), "您正在拨打电话呢!",
-                                Toast.LENGTH_SHORT).show();
-                    }
-                    //					autoCall();
-                } else if (edt1.getText().toString().trim().length() <= 5) {
-                    //不拨打,提示输入号码错误
+        btn2.setOnClickListener(v -> {
+            if (edt1.getText().toString().trim().length() > 5
+                    && edt2.getText().toString().trim().length() > 0) {
+                //如果输入的是电话号码,则拨打
+                number = edt1.getText().toString().trim();
+                Editor editor = sharedPreferences.edit();
+                editor.putString("number", number);
+                editor.apply();
+                amount = Integer.parseInt(edt2.getText().toString().trim());
+                if (thread == null) {
                     Toast.makeText(getApplicationContext(),
-                            "检测到输入的非手机号码,傻眼了吧?", Toast.LENGTH_SHORT).show();
+                            "AutoCall现在带您去自动重拨", Toast.LENGTH_SHORT).show();
+                    thread = new Thread(() -> handler.sendEmptyMessage(0));
+                    thread.start();
+                    Log.i(tag, "thread started");
                 } else {
-                    Toast.makeText(getApplicationContext(), "亲,至少拨打一次呀!",
+                    Toast.makeText(getApplicationContext(), "您正在拨打电话呢!",
                             Toast.LENGTH_SHORT).show();
                 }
+                //					autoCall();
+            } else if (edt1.getText().toString().trim().length() <= 5) {
+                //不拨打,提示输入号码错误
+                Toast.makeText(getApplicationContext(),
+                        "检测到输入的非手机号码,傻眼了吧?", Toast.LENGTH_SHORT).show();
+            } else {
+                Toast.makeText(getApplicationContext(), "亲,至少拨打一次呀!",
+                        Toast.LENGTH_SHORT).show();
             }
         });
-        btn3.setOnClickListener(new OnClickListener() {
-
-            @Override
-            public void onClick(View v) {
-                Toast.makeText(getApplicationContext(), "现在停止自动重拨!", Toast.LENGTH_SHORT).show();
-                StopThread();
-                MainActivity.this.finish();
-            }
+        btn3.setOnClickListener(v -> {
+            Toast.makeText(getApplicationContext(), "现在停止自动重拨!", Toast.LENGTH_SHORT).show();
+            StopThread();
+            MainActivity.this.finish();
         });
     }
 
@@ -171,6 +152,9 @@ public class MainActivity extends Activity {
         }
     };
 
+    /**
+     * PhoneStateListener is deprecated
+     */
     public class MyPhoneStateListener extends PhoneStateListener {
         @Override
         public void onCallStateChanged(int state, String phoneNumber) {

+ 38 - 0
app/src/main/java/com/lyq/autocall/listener/CallReceiver.java

@@ -0,0 +1,38 @@
+package com.lyq.autocall.listener;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+
+/**
+* @Description: 电话广播接收器
+* @Date: 2023年09月25日 Monday
+* @Author liuyuqi.gov@msn.
+*/
+public class CallReceiver extends BroadcastReceiver {
+
+    @Override
+    public void onReceive(Context context, Intent intent) {
+        if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {//如果是去电,呼出
+            String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
+            setResultData(null);//清除去电号码
+            Intent intent1 = new Intent(context, CallService.class);
+            intent1.putExtra("number", phoneNumber);
+            context.startService(intent1);
+        }else(intent.getAction.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)){// 通话状态改变
+            TelephonyManager tm = (TelephonyManager) context.getSystemService(Service.TELEPHONY_SERVICE);
+            switch (tm.getCallState()) {
+                case TelephonyManager.CALL_STATE_RINGING://来电
+                    String incomingNumber = intent.getStringExtra("incoming_number");
+                    Intent intent1 = new Intent(context, CallService.class);
+                    intent1.putExtra("number", incomingNumber);
+                    context.startService(intent1);
+                    break;
+                case TelephonyManager.CALL_STATE_OFFHOOK://接通电话
+                    break;
+                case TelephonyManager.CALL_STATE_IDLE://挂断电话
+                    break;
+            }
+        }
+    }
+}