Android:自动更新我的应用程序

Android:自动更新我的应用程序,android,Android,我是android初学者。我写了一些简单的应用程序,在日历中添加了关于通话的信息。现在我想添加自动更新,如果有人打电话给我,它会自动更新日历 源代码: DateFormat date = new SimpleDateFormat("dd/MM/yyyy hh:mm"); String[] projection = new String[] { "_id", "name" }; ContentValues event = new ContentValues();

我是android初学者。我写了一些简单的应用程序,在日历中添加了关于通话的信息。现在我想添加自动更新,如果有人打电话给我,它会自动更新日历

源代码:

 DateFormat date = new SimpleDateFormat("dd/MM/yyyy hh:mm");
        String[] projection = new String[] { "_id", "name" };
        ContentValues event = new ContentValues();
        long currentTime = System.currentTimeMillis();

        Cursor callCur = getContentResolver().query(android.provider.CallLog.Calls.CONTENT_URI, null, null, null, android.provider.CallLog.Calls.DATE + " DESC");
        Uri calendarsUri = Uri.parse("content://com.android.calendar/calendars");
        Cursor managedCursor = managedQuery(calendarsUri, projection, "selected=1", null, null);

        int typeOfCall = 0;
        String callCachedName = null;
        String callName = null;
        int duration = 0;
        String compareNumbers = null;


        while(callCur.moveToNext()){

        compareNumbers = callCur.getString(callCur.getColumnIndex(android.provider.CallLog.Calls.CACHED_NAME));

        int callDateIn = callCur.getColumnIndexOrThrow(android.provider.CallLog.Calls.DATE);
        long callDateLong = new Long(callCur.getLong(callDateIn));

        event.put("calendar_id", 2);

        typeOfCall = callCur.getInt(callCur.getColumnIndex(android.provider.CallLog.Calls.TYPE));
        Log.d(TAG, "Type of Call: " + typeOfCall);

        if (typeOfCall == 1){
            duration = callCur.getInt(callCur.getColumnIndex(android.provider.CallLog.Calls.DURATION));
            Log.d(TAG, "Duration: " + duration);

            if (duration == 0){
                event.put("description", "Number wasn't avaibled"); 
                }
            else{
                event.put("description", "Duration of call was: " + duration);  
            }
            if (compareNumbers == null){
                callName = callCur.getString(callCur.getColumnIndex(android.provider.CallLog.Calls.NUMBER));
                Log.d(TAG, "callName: " + callName);
                event.put("title", "Called to: " + callName);
            }
            else {
                callCachedName = callCur.getString(callCur.getColumnIndex(android.provider.CallLog.Calls.CACHED_NAME));
                Log.d(TAG, "cachedName: " + callCachedName);
                event.put("title", "Called to: " + callCachedName);
            }

        }
        if (typeOfCall == 2){
            duration = callCur.getInt(callCur.getColumnIndex(android.provider.CallLog.Calls.DURATION));
            Log.d(TAG, "Duration: " + duration);
            if (duration == 0){
                event.put("description", "Call was interupted");    
                }
            else{
                event.put("description", "Duration of call was: " + duration);  
            }
            if (compareNumbers == null){
                callName = callCur.getString(callCur.getColumnIndex(android.provider.CallLog.Calls.NUMBER));
                Log.d(TAG, "callName: " + callName);
                event.put("title", "Call from : " + callName);
            }
            else {
                callCachedName = callCur.getString(callCur.getColumnIndex(android.provider.CallLog.Calls.CACHED_NAME));
                Log.d(TAG, "cachedName: " + callCachedName);
                event.put("title", "Call from : " + callCachedName);
            }

        }
        if (typeOfCall == 3){
             if (compareNumbers == null){
                callName = callCur.getString(callCur.getColumnIndex(android.provider.CallLog.Calls.NUMBER));
                Log.d(TAG, "callName: " + callName);
                event.put("title", "Called to: " + callName);
             }
             else {
                callCachedName = callCur.getString(callCur.getColumnIndex(android.provider.CallLog.Calls.CACHED_NAME));
                Log.d(TAG, "cachedName: " + callCachedName);
                event.put("title", "Called to: " + callCachedName);
             }
             if (compareNumbers == null){
                callName = callCur.getString(callCur.getColumnIndex(android.provider.CallLog.Calls.NUMBER));
                Log.d(TAG, "callName: " + callName);
                event.put("title", "Call from : " + callName);
             }
             else {
                callCachedName = callCur.getString(callCur.getColumnIndex(android.provider.CallLog.Calls.CACHED_NAME));
                Log.d(TAG, "cachedName: " + callCachedName);
                event.put("title", "Call from : " + callCachedName);
             }


        }

        //event.put("eventLocation", "New Yorks");
        event.put("dtstart", callDateLong );


       Uri eventsUri = Uri.parse("content://com.android.calendar/events");
       Uri calUri = getContentResolver().insert(eventsUri, event);

        }
    }
}

扩展
BroadcastReceiver
并使用
ACTION\u ANSWER
intent过滤器
在您的
AndroidManifest.xml
中添加一个条目。然后,您可以从
myBroadcastReceiver
类的
onReceive
方法调用日历更新方法