如何以编程方式启动 PhoneStateListener?

发布于 2025-01-06 22:06:13 字数 168 浏览 0 评论 0原文

我的应用程序中有一项活动。它包含一个按钮。通过单击按钮,应该启动 PhoneStateListener(和 BroadcastReceiver?)来捕获传入和传出呼叫。看来应该是一个服务。

有谁可以解释如何以编程方式启动 PhoneStateListener (和 BroadcastReceiver?)?

There's an activity in my application. It contains a button. By clicking the button it should be start PhoneStateListener (and BroadcastReceiver?) to catch incoming and outgoing calls. It seems it should be a service.

Does anyone can explain how to start PhoneStateListener (and BroadcastReceiver?) programmatically?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

满地尘埃落定 2025-01-13 22:06:14

试试这个:

public class myActivity extends Activity{

private TelephonyManager telephonyManager = null;

public void onCreate(Bundle savedInstanceState) {

    // setcontentview and other

    button.setOnClickListener(new OnClickListener(){
           public void onClick(View arg0) {
               btnClick();       
           }
    });

}

private void btnClick(){

telephonyManager = (TelephonyManager)getApplicationContext()
                .getSystemService(Context.TELEPHONY_SERVICE);
        telephonyManager.listen(new PhoneStateListener(){

            @Override
            public void onCallStateChanged(int state, String incomingNumber) {
                switch(state){
                    case TelephonyManager.CALL_STATE_IDLE:
                        /*your code*/
                        break;
                    case TelephonyManager.CALL_STATE_OFFHOOK:
                        /*your code*/
                        break;
                    case TelephonyManager.CALL_STATE_RINGING:
                        /*your code*/
                        break;
                }
                //super.onCallStateChanged(state, incomingNumber);
            }

        }, PhoneStateListener.LISTEN_CALL_STATE);

    } 

}

Try this:

public class myActivity extends Activity{

private TelephonyManager telephonyManager = null;

public void onCreate(Bundle savedInstanceState) {

    // setcontentview and other

    button.setOnClickListener(new OnClickListener(){
           public void onClick(View arg0) {
               btnClick();       
           }
    });

}

private void btnClick(){

telephonyManager = (TelephonyManager)getApplicationContext()
                .getSystemService(Context.TELEPHONY_SERVICE);
        telephonyManager.listen(new PhoneStateListener(){

            @Override
            public void onCallStateChanged(int state, String incomingNumber) {
                switch(state){
                    case TelephonyManager.CALL_STATE_IDLE:
                        /*your code*/
                        break;
                    case TelephonyManager.CALL_STATE_OFFHOOK:
                        /*your code*/
                        break;
                    case TelephonyManager.CALL_STATE_RINGING:
                        /*your code*/
                        break;
                }
                //super.onCallStateChanged(state, incomingNumber);
            }

        }, PhoneStateListener.LISTEN_CALL_STATE);

    } 

}
兲鉂ぱ嘚淚 2025-01-13 22:06:14

你必须使用这段代码,它是 100% 有效的。

(1) 您必须启动服务

startService(new Intent(this, CallServices.class));

(2) 您必须创建 CallServices 类并启动您的内容观察器。

public class CallServices extends Service{
private static final String TAG = "CallService";
Context mContext;

@Override
public IBinder onBind(Intent intent) {
    Log.d(TAG, "onBind");
    return null;
}

@Override
public void onCreate() {
    Log.d(TAG, "onCreate");
    mContext=getApplicationContext();   
}

@Override
public void onStart(Intent intent, int startId) {
    //super.onStart(intent, startId);
    Log.d(TAG, "onStart services is started");
       Uri mediaUri = Uri.parse("content://call_log/calls");
       Handler handler = new Handler(){};
       CustomContentObserver custObser = new CustomContentObserver(handler,mContext);        
       mContext.getContentResolver().registerContentObserver(mediaUri, true, custObser);    
}

(3 )

创建 CustomContentObserver 类来获取通话日志。

public class CustomContentObserver extends ContentObserver {
long lastTimeofCall = 0L;
long lastTimeofUpdate = 0L;
long threshold_time = 10000;

public CustomContentObserver(Handler handler,Context con) {
    super(handler);
    this.mContext=con;
    System.out.println("Content obser");
    callflag=true;
}     
@Override 
public boolean deliverSelfNotifications() { 
    return false; 
}

public void onChange(boolean selfChange) {
 super.onChange(selfChange);

 lastTimeofCall = System.currentTimeMillis();
    if(lastTimeofCall - lastTimeofUpdate > threshold_time){         
        if(callflag){           

             System.out.println("Content obser onChange()");
             Log.d("PhoneService", "custom StringsContentObserver.onChange( " + selfChange + ")");
            //if(!callFlag){                   
             String[] projection = new String[]{CallLog.Calls.NUMBER,
                        CallLog.Calls.TYPE,
                        CallLog.Calls.DURATION,
                        CallLog.Calls.CACHED_NAME,
                        CallLog.Calls._ID};         
          Cursor c;
          c=mContext.getContentResolver().query(CallLog.Calls.CONTENT_URI, projection, null, null, CallLog.Calls._ID + " DESC");
          if(c.getCount()!=0){
                c.moveToFirst();
                 lastCallnumber = c.getString(0);
                 lastCallnumber=FormatNumber(lastCallnumber);                     
                 String type=c.getString(1);
                 String duration=c.getString(2);
                 String name=c.getString(3);
                 String id=c.getString(4);
                 System.out.println("CALLLLing:"+lastCallnumber+"Type:"+type);

                 Database db=new Database(mContext);
                 Cursor cur =db.getFirstRecord(lastCallnumber);
                 System.out.println("CUSTOM GALLARY..."+cur.getCount());
                 final String endCall=lastCallnumber;
                 //checking incoming/outgoing call
                 if(type.equals("1")){
                    //incoming call code

                 }else if(type.equals("2")){
                        //outgoing call code
                 } else{
                    //missed call code
                }
            }

        lastTimeofUpdate = System.currentTimeMillis();
    }
}

这样

就必须读取通话记录数据。
我正在使用内容观察者,因为在 htc 设备中无法读取phonestateListner。

you have to used this code and it is 100% work.

(1) you have to start services

startService(new Intent(this, CallServices.class));

(2) you have to make CallServices class and start your contentobser.

public class CallServices extends Service{
private static final String TAG = "CallService";
Context mContext;

@Override
public IBinder onBind(Intent intent) {
    Log.d(TAG, "onBind");
    return null;
}

@Override
public void onCreate() {
    Log.d(TAG, "onCreate");
    mContext=getApplicationContext();   
}

@Override
public void onStart(Intent intent, int startId) {
    //super.onStart(intent, startId);
    Log.d(TAG, "onStart services is started");
       Uri mediaUri = Uri.parse("content://call_log/calls");
       Handler handler = new Handler(){};
       CustomContentObserver custObser = new CustomContentObserver(handler,mContext);        
       mContext.getContentResolver().registerContentObserver(mediaUri, true, custObser);    
}

}

(3) make CustomContentObserver class to getting you to call log.

public class CustomContentObserver extends ContentObserver {
long lastTimeofCall = 0L;
long lastTimeofUpdate = 0L;
long threshold_time = 10000;

public CustomContentObserver(Handler handler,Context con) {
    super(handler);
    this.mContext=con;
    System.out.println("Content obser");
    callflag=true;
}     
@Override 
public boolean deliverSelfNotifications() { 
    return false; 
}

public void onChange(boolean selfChange) {
 super.onChange(selfChange);

 lastTimeofCall = System.currentTimeMillis();
    if(lastTimeofCall - lastTimeofUpdate > threshold_time){         
        if(callflag){           

             System.out.println("Content obser onChange()");
             Log.d("PhoneService", "custom StringsContentObserver.onChange( " + selfChange + ")");
            //if(!callFlag){                   
             String[] projection = new String[]{CallLog.Calls.NUMBER,
                        CallLog.Calls.TYPE,
                        CallLog.Calls.DURATION,
                        CallLog.Calls.CACHED_NAME,
                        CallLog.Calls._ID};         
          Cursor c;
          c=mContext.getContentResolver().query(CallLog.Calls.CONTENT_URI, projection, null, null, CallLog.Calls._ID + " DESC");
          if(c.getCount()!=0){
                c.moveToFirst();
                 lastCallnumber = c.getString(0);
                 lastCallnumber=FormatNumber(lastCallnumber);                     
                 String type=c.getString(1);
                 String duration=c.getString(2);
                 String name=c.getString(3);
                 String id=c.getString(4);
                 System.out.println("CALLLLing:"+lastCallnumber+"Type:"+type);

                 Database db=new Database(mContext);
                 Cursor cur =db.getFirstRecord(lastCallnumber);
                 System.out.println("CUSTOM GALLARY..."+cur.getCount());
                 final String endCall=lastCallnumber;
                 //checking incoming/outgoing call
                 if(type.equals("1")){
                    //incoming call code

                 }else if(type.equals("2")){
                        //outgoing call code
                 } else{
                    //missed call code
                }
            }

        lastTimeofUpdate = System.currentTimeMillis();
    }
}

}

}

in this way you have to read call log data.
i am using content Observer because of in htc device can not read phonestateListner that's way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文