开发者

Android: Automatic update of my app

开发者 https://www.devze.com 2023-02-24 08:37 出处:网络
I am beginner android developer.I wrote some simple app,witch adding to the calendar informations about calls.Now i want to add automatic update,if somebody call me,it automaticly update calendar.

I am beginner android developer.I wrote some simple app,witch adding to the calendar informations about calls.Now i want to add automatic update,if somebody call me,it automaticly update calendar.

Source code:

 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: " 开发者_StackOverflow社区+ 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);

        }
    }
}


Extend BroadcastReceiver and add an entry in your AndroidManifest.xml with an intent-filter of ACTION_ANSWER. You can then call your calender update method from the onReceive method of your myBroadcastReceiver class.

0

精彩评论

暂无评论...
验证码 换一张
取 消