Android内容查看器-onChange方法检查上一个呼叫是否未接 [英] android content observer - onChange method check if last call is missed call or not

查看:103
本文介绍了Android内容查看器-onChange方法检查上一个呼叫是否未接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Android应用,我需要增加未接来电。



我注册了ContentObserver。



我有一个带有以下代码的contentobserver:

 公共类IncomingCall扩展BroadcastReceiver 
{
public void onReceive(最终上下文上下文,意图)
{
字符串状态= extras.getString(TelephonyManager.EXTRA_STATE);
if(TelephonyManager.EXTRA_STATE_IDLE.equals(state))
{
context.getApplicationContext()。getContentResolver()。registerContentObserver(android.provider.CallLog.Calls.CONTENT_URI,true,新的CallContentObserver (新的Handler(),上下文));
}

}

类CallContentObserver扩展ContentObserver {

上下文上下文;

public CallContentObserver(Handler handler,Context context){
super(handler);
this.context =上下文;
}

@Override
public boolean deliverySelfNotifications(){
返回true;
}

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

//未接电话标志
如何检查上一个电话是否为未接电话?

游标c = context.getContentResolver()。query(CallLog.Calls.CONTENT_URI,null,null,null,null);
int类型= c.getColumnIndex(CallLog.Calls.TYPE);
而(c.moveToNext()){
字符串callType = c.getString(type);

int目录代码= Integer.parseInt(callType);
switch(dircode){

case CallLog.Calls.MISSED_TYPE:
flag ++;

}
休息;
}

}

没有其他方法可以做这个? (比我检查CallLog数据库的上一次呼叫更好吗?

解决方案

我建议尝试以下代码以确定是否错过了最后一个呼叫:

  @Override 
public void onChange(boolean selfChange,Uri uri){

if(null == uri){
onChange(selfChange);
return;
}

super.onChange (selfChange,uri);

最终游标c = context.getContentResolver()。query(uri,null,null,null,null);
最终int类型= c.getColumnIndex(CallLog .Calls.TYPE);
final int dircode = c.getInt(type);

switch(dircode){
case CallLog.Calls.MISSED_TYPE:
flag ++ ;
休息;
}
}

而不是检查所有呼叫,以防万一您只需要检查最新呼叫。但是,如果uri不在,则提供的方法看起来不错。



另一个建议是实施Service来检查未接来电计数,因此您在 ContentObserver 中将不会进行长时间处理。 / p>

I am working on an android app and I need to increment the incoming missed calls.

I registered a ContentObserver. How can I check in the onChange method if the call is a missed call or not?

I have a contentobserver with the following code:

public class IncomingCall extend BroadcastReceiver
{
public void onReceive( final Context context, Intent intent)
{
String state= extras.getString(TelephonyManager.EXTRA_STATE);
    if (TelephonyManager.EXTRA_STATE_IDLE.equals(state))
        {
            context.getApplicationContext().getContentResolver().registerContentObserver(android.provider.CallLog.Calls.CONTENT_URI, true, new CallContentObserver(new Handler(), context));
        }

}

 class CallContentObserver extends ContentObserver {

    Context context;

    public CallContentObserver(Handler handler, Context context) {
        super(handler);
        this.context = context;
    }

    @Override
    public boolean deliverSelfNotifications() {
        return true;
    }

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

        //flag for missed calls
        how to check if last call is missed call?

Cursor c = context.getContentResolver().query(CallLog.Calls.CONTENT_URI,null,null, null, null);
        int type = c.getColumnIndex(CallLog.Calls.TYPE);
        while (c.moveToNext()) {
            String callType = c.getString(type);

            int dircode = Integer.parseInt(callType);
            switch (dircode) {

            case CallLog.Calls.MISSED_TYPE:
                flag++;

            }
            break;
        }

}

Isn't there another way to do this? (better than I did when checking the last call from CallLog database?

解决方案

I would suggest to try the following code in order to determine 'if last call is missed call or not':

@Override
public void onChange(boolean selfChange, Uri uri) {

    if (null == uri) {
        onChange(selfChange);
        return;
    }

    super.onChange(selfChange, uri);

    final Cursor c = context.getContentResolver().query(uri,null,null, null, null);
    final int type = c.getColumnIndex(CallLog.Calls.TYPE);
    final int dircode = c.getInt(type);

    switch (dircode) {
        case CallLog.Calls.MISSED_TYPE:
            flag++;
            break;
    }
}

It would be faster than checking all calls in case if You need to check only latest call. However, if where will be no uri, then provided way looks fine.

Another suggestion is to implement Service to do checking of missed calls count, so You will not have long processing in ContentObserver.

这篇关于Android内容查看器-onChange方法检查上一个呼叫是否未接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆