如何在android中调用end后启动一个Activity?

时间:2014-07-23 06:22:54

标签: android android-intent android-activity

我想在收到电话结束后发送短信,E.G。我接到一个电话,我想在电话会议后自动发送他/她的消息,当我将我的课程扩展到PhoneStateListener时,我无法开始活动,请还原我

2 个答案:

答案 0 :(得分:1)

您需要实施broadcastreceiver

<receiver android:name=".PhoneStateBroadcastReceiver">
<intent-filter>
    <action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>

你应该创建一个BroadcastReceiver:

package com.mobisys.android.salesbooster;

import com.mobisys.android.salesbooster.database.HelperDatabase;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.ContactsContract.PhoneLookup;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;

public class PhoneStateBroadcastReceiver extends BroadcastReceiver {

    private static final String TAG = "PhoneStateBroadcastReceiver";
    Context mContext;
    String incoming_number;
    private int prev_state;

    @Override
    public void onReceive(Context context, Intent intent) {
        TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); //TelephonyManager object
        CustomPhoneStateListener customPhoneListener = new CustomPhoneStateListener();
        telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE); //Register our listener with TelephonyManager

        Bundle bundle = intent.getExtras();
        String phoneNr = bundle.getString("incoming_number");
        Log.v(TAG, "phoneNr: "+phoneNr);
        mContext = context;
    }

    /* Custom PhoneStateListener */
    public class CustomPhoneStateListener extends PhoneStateListener {

        private static final String TAG = "CustomPhoneStateListener";

        @Override
        public void onCallStateChanged(int state, String incomingNumber){

           if( incomingNumber != null && incomingNumber.length() > 0 ) 
            incoming_number = incomingNumber; 

            switch(state){
                case TelephonyManager.CALL_STATE_RINGING:
                        Log.d(TAG, "CALL_STATE_RINGING");
                        prev_state=state;
                        break;

                case TelephonyManager.CALL_STATE_OFFHOOK:
                                Log.d(TAG, "CALL_STATE_OFFHOOK");
                                prev_state=state;
                                break;

                case TelephonyManager.CALL_STATE_IDLE:

                    Log.d(TAG, "CALL_STATE_IDLE==>"+incoming_number);

                    if((prev_state == TelephonyManager.CALL_STATE_OFFHOOK)){
                        prev_state=state;
                        //Answered Call which is ended
                    }
                    if((prev_state == TelephonyManager.CALL_STATE_RINGING)){
                        prev_state=state;
                        //Rejected or Missed call
                    }
                    break;
            }
        }
    }
}

在此处阅读更多内容,来源:http://mobisys.in/blog/2011/09/is-your-call-ended-on-android-phone/

答案 1 :(得分:1)

您需要为此实现service / broadcastreceiver。它将收到呼叫结束的系统通知。所以你需要处理那个通知&amp;一旦你得到它。然后,您可以在收到通知时开始发送消息。

所需组件:

  1. 服务

  2. 广播接收器