简体   繁体   English

在Android的Google日历中添加多个事件

[英]Adding several events in Google Calendar on Android

I'm writting ab android application that handles the end of a call: 我正在写一个处理呼叫结束的android应用程序:

  • when you hangup, it sends a notification 挂断电话时,它会发送通知

  • if you click on that notification it takes you to the Google Calendar new event UI, 如果您点击该通知,它将带您进入Google日历新事件界面,

  • the field title is filled with the phone number or name of the contact. 字段标题将填充联系人的电话号码或姓名。

My problem is: 我的问题是:

If you are called by numer +442070313000 call -> notification pop up, and when you click, it creates an event with title +442070313000 如果您通过电话+442070313000呼叫->弹出通知,当您单击时,它将创建一个标题为+442070313000的事件

But if then number 666 calls -> notification pop up, but calendar event is filled with the number +442070313000! 但是,如果弹出666呼叫->通知弹出,但日历事件将填充为+442070313000!

I debugged the application and the intent passed to the google calendar is ok. 我调试了该应用程序,并且可以将传递给Google日历的意图确定为可以。 But it seems to keep only the first intent and ignore other ones 但这似乎只保留第一个意图,而忽略其他意图

Here is the code that handle event : 这是处理事件的代码:

package com.amos.addincalendar.facade;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;

public class CallReceiverIncoming extends BroadcastReceiver {

    String LOG_TAG = "CallReceiver";
    String EVENT_TYPE_PHONE_STATE_CHANGED = "phone state changed";
    private static int MIN_TOTAL_TIME = 0;
    static String LOG_TAG = "CallReceiver";

    TelephonyManager tm;
    private String sPhoneNumber = "";
    private boolean bAgenda = false;
    private long lStartTime;
    private long lEndTime;

    private void onReceivePhoneStateChanged(final Context context,
              final Intent intent) {

        final String phoneState = intent.getStringExtra(TelephonyManager.EXTRA_STATE);

        final StringBuilder sb = new StringBuilder();

        if (TelephonyManager.EXTRA_STATE_IDLE.equals(phoneState)) {
            Log.d(LOG_TAG, "event : idle");
            bAgenda = true;
            lEndTime = System.currentTimeMillis();
        } else if (TelephonyManager.EXTRA_STATE_OFFHOOK.equals(phoneState)) {
            Log.d(LOG_TAG, "event : offhook");
            bAgenda = true;
            lEndTime = System.currentTimeMillis();
        } else if (TelephonyManager.EXTRA_STATE_RINGING.equals(phoneState)) {
            Log.d(LOG_TAG, "event : ringing");
            lStartTime = System.currentTimeMillis();
        }

    }



    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();

        if(null == bundle){
            return;         
        }

        final String action = intent.getAction();
        sPhoneNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);

        if (TelephonyManager.ACTION_PHONE_STATE_CHANGED.equals(action)) {
            onReceivePhoneStateChanged(context, intent);
        } else {
            final String data = intent.getDataString();

            Log.v(LOG_TAG, "broadcast : action=" + action + ", data=" + data);
        }


        if(!bAgenda){
            CallReceiver.agendaFishAndChips(context, sPhoneNumber, lStartTime, lEndTime);
        }

    }


    public static void agendaFishAndChips(Context context, String sPhoneNumber, long lStartTime, long lEndTime){

        StringBuilder sbTitleEvent  = new StringBuilder();
        StringBuilder sbDescription = new StringBuilder();
        StringBuilder sbAddress     = new StringBuilder();
        StringBuilder sbName        = new StringBuilder();

        if(sPhoneNumber != null && !sPhoneNumber.equals("")){
            sbTitleEvent.append(sPhoneNumber);

            CallReceiver.sendNotificationAgenda(context, sbTitleEvent.toString(), sbAddress.toString(), sbDescription.toString(), sPhoneNumber, sbName.toString());

        }
    }

    public static String getCaracteresNumeriques (String chaine){
        String sChaineNumerique = "";

        Matcher matcher = Pattern.compile("[0-9]*").matcher(chaine);
        while ( matcher.find() ) {
            sChaineNumerique += matcher.group();
        }
        return sChaineNumerique;
    }

    /*
     * Send a notification and add event in agenda
     */
    public static void sendNotificationAgenda(Context context, String sTitleEvent, String sAddress, String sDescription, String sPhoneNumber, String sName){
        NotificationManager notificationManager;
        notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);

        sName = (sName != null && !sName.equals("") ? sName : sPhoneNumber);
        String sNotificationTitle = "Rendezvous with "+ sName;
        Notification notification = new Notification(R.drawable.ic_launcher, sNotificationTitle, System.currentTimeMillis());

        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.defaults |= Notification.DEFAULT_LIGHTS;

        Log.d(LOG_TAG, sName + " : " + getCaracteresNumeriques(sPhoneNumber));

        Log.d(LOG_TAG, sTitleEvent);

    //Create the google event intent
    Intent intentNotification = new Intent(Intent.ACTION_EDIT);
        intentNotification.setType("vnd.android.cursor.item/event");

        intentNotification.putExtra("title", sTitleEvent);
        intentNotification.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    Bundle objetbunble = new Bundle();
    objetbunble.putString("TitleEvent"  , sTitleEvent);
    objetbunble.putString("Address"     , sAddress);
    objetbunble.putString("Description", sDescription);
    intentNotification.putExtras(objetbunble);

        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intentNotification, 0);
        notification.setLatestEventInfo(context,
                    sNotificationTitle,
                    context.getText(R.string.notification_new_rdv) + " " + sName,   pendingIntent);
        notificationManager.notify( Integer.parseInt(getCaracteresNumeriques(sPhoneNumber)), notification);


    }


    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        sPhoneNumber = "";
    }
}

And here is the manifest : 这是清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.amos.addincalendar"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />
    <uses-permission android:name="android.permission.WRITE_CALENDAR"/>
    <uses-permission android:name="android.permission.READ_CALENDAR"/>
    <uses-permission android:name="android.permission.WRITE_SMS"/>
    <uses-permission android:name="android.permission.SET_DEBUG_APP"/>
    <uses-permission android:name="android.permission.READ_CONTACTS"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.DeviceDefault" >

          <activity
            android:name=".action.CalendarMain"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


        <receiver android:name=".facade.CallReceiverIncoming">
            <intent-filter> 
                <category android:name="android.intent.category.DEFAULT" />
                <action android:name="android.intent.action.PHONE_STATE"/>
            </intent-filter> 
        </receiver>

        <activity android:name=".dao.CalendarDAO"></activity>
        <activity android:name=".facade.CallReceiver"></activity>
        <activity android:name=".facade.CallActivity" android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen"></activity>

    </application>
</manifest>

I think in this case you have to provide each PendingIntent with an unique id. 我认为在这种情况下,您必须为每个PendingIntent提供唯一的ID。 Try replacing the 0 in 尝试将0替换为

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intentNotification, 0); 

with a unique ID (perhaps the phone number?). 具有唯一的ID(也许是电话号码?)。 If you provide the same number to each PendingIntent then the same intent (first one sent) will be read. 如果为每个PendingIntent提供相同的编号,则将读取相同的Intent(发送的第一个Intent)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM