2

我正在编写处理呼叫结束的 ab android 应用程序:

  • 当您挂断时,它会发送通知

  • 如果您单击该通知,它会将您带到 Google 日历的新活动用户界面,

  • 字段标题填写联系人的电话号码或姓名。

我的问题是:

如果您被号码 +442070313000 呼叫 -> 弹出通知,并且当您单击时,它会创建一个标题为 +442070313000 的事件

但是如果然后号码 666 呼叫 - > 通知弹出,但日历事件填充号码 +442070313000!

我调试了应用程序,传递给谷歌日历的意图是好的。但它似乎只保留第一个意图而忽略其他意图

这是处理事件的代码:

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 = "";
    }
}

这是清单:

<?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>
4

1 回答 1

1

我认为在这种情况下,您必须为每个 PendingIntent 提供一个唯一的 ID。尝试将 0 替换为

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

具有唯一 ID(可能是电话号码?)。如果您为每个 PendingIntent 提供相同的编号,则将读取相同的意图(发送的第一个)。

于 2012-09-21T11:01:40.363 回答