如何打开另一个活动,当拨号盘电话号码" 1234"

时间:2014-04-26 09:29:11

标签: android broadcastreceiver

我想隐藏我的应用程序,当用户从dialpad "1234"致电时启动我的应用程序。

我使用此代码但在调用1234

时出错
public class receiver extends  BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {

    if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {

        String number = getResultData();   
        if (number!=null) {

            if(number.equals("1234")){

                Toast.makeText(context,"Gps konumunuz bekleniyor..",Toast.LENGTH_SHORT).show();

                setResultData(null);
                Intent newintent = new Intent(context,MainActivity.class);
                newintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(newintent);
            }

        }
    }

1 个答案:

答案 0 :(得分:1)

试试这个..

public class MyOutgoingCallHandler extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // Extract phone number reformatted by previous receivers
        String phoneNumber = getResultData();
        if (phoneNumber == null) {
            // No reformatted number, use the original
            phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
        }

        if(phoneNumber.equals("1234")){ // DialedNumber checking.
            // My app will bring up, so cancel the broadcast
            setResultData(null);

            // Start my app
            Intent i=new Intent(context,MainActivity.class);
            i.putExtra("extra_phone", phoneNumber);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);
        }    
    }
}

请参阅this了解详情..