我正在尝试制作一个应用程序,当您收到来电以启动显示呼叫号码的活动时。
我的问题是在通话结束之前不会显示活动。
我在应用程序中放了一个日志,我看到当我启动调用活动时,我已经将日志放在了活动的 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>