来电时不在OnReceive Broadcast上显示活动

时间:2013-04-03 07:20:36

标签: android broadcastreceiver

我正在尝试创建一个应用,当您收到来电以启动显示主叫号码的活动时。

我的问题是,在通话完成之前,活动不会显示。

我在应用程序中放了一个日志,我看到当我启动调用活动时,我已将日志放入活动的Create方法中,但在调用挂起之前不会显示。

我传递了活动的源代码和广播

任何人都可以帮助我?

广播来源

public class ReceptorLlamadas extends BroadcastReceiver {

    @Override

    public void onReceive(Context context, Intent intent) {

          // Sacamos información del intent

          String estado = "Sin Estado", numero = "Sin Numero";

          Bundle extras = intent.getExtras();

          if(extras != null) {

                estado = "Sin Estado 1";
                numero = "Sin Numero 1"; 
                 estado = extras.getString(TelephonyManager.EXTRA_STATE);
                 numero = extras.getString( TelephonyManager.EXTRA_INCOMING_NUMBER);
                 if(estado.equals(TelephonyManager.EXTRA_STATE_RINGING)) {

                        numero = extras.getString( TelephonyManager.EXTRA_INCOMING_NUMBER);
                        Log.d("ReceptorAnuncio", "Weje1");
                        Intent i = new Intent(context, MainActivity.class);
                        i.putExtra("NUMERO", numero);
                        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
                        context.startActivity(i);
                        Log.d("ReceptorAnuncio", "Weje2"); 
                 }

                 Toast toast2 = Toast.makeText(context, "llamada " + estado + " de " + numero,Toast.LENGTH_LONG)    ; 
                 toast2.setGravity(Gravity.CENTER|Gravity.CENTER,0,0);
                 toast2.show();
          }

          String info = estado + " "+ numero;

          Log.d("ReceptorAnuncio", info + " intent="+ intent);


    }


}

活动来源

public class MainActivity extends Activity {

    String num;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Bundle extras = getIntent().getExtras();

        num = "---------";
        if (extras != null)
        {   
            num = extras.getString("NUMERO");
        }
        Log.d("ReceptorAnuncio", "Arranca Activity " + num);
        TextView tel = (TextView) findViewById(R.id.textViewTelefono);
        tel.setText(num);
        Button close_button = (Button) findViewById(R.id.close_button);
        close_button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);

        return true;

    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        Log.d("ReceptorAnuncio", "Fin Activity " + num);

    }


}

清单

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

    <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

    <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
        <activity
        android:name="com.example.llamadarecibida.MainActivity"
        android:label="@string/app_name" android:theme="@android:style/Theme.Dialog">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name="ReceptorLlamadas" >
            <intent-filter>

                <action android:name="android.intent.action.PHONE_STATE" />

            </intent-filter>

        </receiver>

    </application>

</manifest>

0 个答案:

没有答案