创建未接来电通知

时间:2012-02-04 19:53:13

标签: android

我正在尝试创建一个应用程序,当我被调用为什么我的手机关闭时,会在日志中添加未接来电。我的提供商向我发送了一封来自该人的文字,例如Missed call: this person/number called at 23:46 on 28th Jan but left no message.,所以我创建了一个广播接收器来处理消息的接收。这部分工作正常。但是,我还希望能够显示有未接来电的通知,并增加未接来电的数量。有没有办法显示通知?

当前代码:

package uk.me.dtwood.missedcalls;

import java.util.Calendar;

import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.CallLog;
import android.telephony.SmsMessage;
import android.util.Log;

public class MissedCallsReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras(); 
        Object[] pdusObj = (Object[]) bundle.get("pdus"); 
        SmsMessage[] messages = new SmsMessage[pdusObj.length]; 
        for (int i = 0; i < pdusObj.length; i++) { 
            messages[i] = SmsMessage.createFromPdu ((byte[]) 
                    pdusObj[i]);  // AARGH! - see Javadoc for createFromPdu().
        }

        String testString = "Missed call: this person/number called at 23:46 on 28th Jan but left no message.";
        if (messages[0].getMessageBody().equals(testString))
        {
            abortBroadcast();
            ContentResolver cr = context.getContentResolver();
            Calendar callTime = Calendar.getInstance();
            insertPlaceholderCall(cr, messages[0].getOriginatingAddress(), callTime.getTimeInMillis());
        }
    }

    public static void insertPlaceholderCall(ContentResolver contentResolver, String number, Long addTime){
        ContentValues values = new ContentValues();
        values.put(CallLog.Calls.NUMBER, number);
        values.put(CallLog.Calls.DATE, addTime);
        values.put(CallLog.Calls.DURATION, 0);
        values.put(CallLog.Calls.TYPE, CallLog.Calls.MISSED_TYPE);
        values.put(CallLog.Calls.NEW, 1);
        values.put(CallLog.Calls.CACHED_NAME, "");
        values.put(CallLog.Calls.CACHED_NUMBER_TYPE, 0);
        values.put(CallLog.Calls.CACHED_NUMBER_LABEL, "");
        Log.d("DEBUG", "Inserting call log placeholder for " + number + " at " + addTime);
        contentResolver.insert(CallLog.Calls.CONTENT_URI, values);
    }
}

0 个答案:

没有答案