有人可以告诉我 2024 年是否可以读取手机状态吗?

问题描述 投票:0回答:1

有人可以告诉我现在在 2024 年是否可以读取手机状态,因为我在 2023 年 10 月有一个广播监听器,它会监听正在执行的呼叫,并且一旦呼叫结束,它就会发送呼叫详细信息,例如持续时间、呼叫键入到服务器。但是现在当我测试相同的代码时,它不起作用,呼叫结束后不会调用广播侦听器。我正在 Android 13 设备上测试它。我已附上该侦听器的代码。非常感谢您的帮助。

public class InfluencerCallBroadcastReceiver extends BroadcastReceiver {
    private static final String TAG = "InfluencerCallListener";

    ArrayList<CallLogModel> callLog;
    private static CallLogListener callLogListener;

    CallLogModel callLogModel;
    InfluencerCallReceiver influencerCallReceiver;

    public interface InfluencerCallReceiver{
        void onItemClicked(String number, ArrayList<CallLogModel> log);
    }

    public InfluencerCallBroadcastReceiver(){

    }
    public InfluencerCallBroadcastReceiver(InfluencerCallReceiver influencerCallReceiver) {
        this.influencerCallReceiver = influencerCallReceiver;
    }

    @Override
    public void onReceive(Context context, Intent intent) {

        Bundle bundle = intent.getExtras();
        Log.d(TAG, "run: "+bundle);
        if (bundle.getString("incoming_number")!=null && Objects.equals(bundle.getString("state"), "IDLE")){
            String phoneNr = bundle.getString("incoming_number");
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    getCallDetails(context,phoneNr);

                }
            }, 2000);
        }
    }

    private void getCallDetails(Context context, String phone) {
        callLog = new ArrayList<>();
        Log.d("phondata", "getCallDetails: "+phone);

        String timestamp = String.valueOf(getTodayTimestamp());

        StringBuffer sb = new StringBuffer();

        Cursor managedCursor = context.getContentResolver().query ( CallLog.Calls.CONTENT_URI,
                null,CallLog.Calls.DATE + ">= ? AND " + CallLog.Calls.NUMBER + " = ?" ,
                new String[]{phone}, CallLog.Calls.DATE + " DESC");

        int number = managedCursor.getColumnIndex( CallLog.Calls.NUMBER );
        int type = managedCursor.getColumnIndex( CallLog.Calls.TYPE );
        int date = managedCursor.getColumnIndex( CallLog.Calls.DATE);
        int duration = managedCursor.getColumnIndex( CallLog.Calls.DURATION);
        sb.append( "Call Details :");
        Log.d("ShowDate", "getCallDetails: "+managedCursor.getCount());

        if ( managedCursor.moveToFirst() ) {
            String phNumber = managedCursor.getString( number );
//            String phNumber = phone;
            String callType = managedCursor.getString( type );
//            String callDate = String.valueOf(date1);
            String callDate = managedCursor.getString( date );

            Date callDayTime = new Date(Long.parseLong(callDate));
            String callDuration = managedCursor.getString( duration );
            String dir = null;
            int dircode = Integer.parseInt( callType );
            switch( dircode ) {
                case CallLog.Calls.OUTGOING_TYPE:
                    dir = "OUTGOING";
                    break;

                case CallLog.Calls.INCOMING_TYPE:
                    dir = "INCOMING";
                    break;

                case CallLog.Calls.MISSED_TYPE:
                    dir = "MISSED";
                    break;
            }
            callLogModel = new CallLogModel();
            callLogModel.setPhoneNumber(phNumber);
            callLogModel.setCallType(dir);
            callLogModel.setCallDayTime(String.valueOf(callDayTime));
            callLogModel.setCallDuration(callDuration);

            callLog.add(callLogModel);

            managedCursor.close();
            sb.append( "\nPhone Number:--- "+phone +" \nCall Type:--- "+dir+" \nCall Date:--- "+callDayTime+" \nCall duration in sec :--- "+callDuration );
            sb.append("\n----------------------------------");
        }else {
            Log.d(TAG, "No call log found for the number: " + phone);
        }

        Log.d(TAG, "getCallDetails: "+sb);

        if (influencerCallReceiver != null){
            influencerCallReceiver.onItemClicked(phone,callLog);
        }
    }

    //With this method you will get the timestamp of today at midnight
    public long getTodayTimestamp(){
        Calendar c1 = Calendar.getInstance();
        c1.setTime(new Date());

        Calendar c2 = Calendar.getInstance();
        c2.set(Calendar.YEAR, c1.get(Calendar.YEAR));
        c2.set(Calendar.MONTH, c1.get(Calendar.MONTH));
        c2.set(Calendar.DAY_OF_MONTH, c1.get(Calendar.DAY_OF_MONTH));
        c2.set(Calendar.HOUR_OF_DAY, 0);
        c2.set(Calendar.MINUTE, 0);
        c2.set(Calendar.SECOND, 0);

        return c2.getTimeInMillis();
    }

}

我检查了是否授予了所有必要的权限,在多个设备上进行了测试,我尝试使用 logcat 调试代码来检查广播接收器是否被击中,但没有任何效果。我最初认为发生这种情况是因为我创建了两个不同的广播监听器来监听手机状态,但后来我删除了其中一个。它仍然不起作用。 以下是清单代码。

<receiver
            android:name=".CallListener"
            android:exported="true">
            <intent-filter android:priority="2147483647">
                <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
            </intent-filter>
        </receiver>```
android broadcastreceiver phone-state-listener
1个回答
0
投票

不足以在清单中声明,因为这是一个危险的许可。您需要提示用户授予权限。 https://developer.android.com/training/permissions/requesting

© www.soinside.com 2019 - 2024. All rights reserved.