我在應用程序中完成了通話記錄,並且在手機處於振鈴狀態時打電話給一個Activity。在該活動中,只顯示了電話號碼和人名。爲此,我在我的類中編寫了代碼,它擴展了BroadcastReceiver。BroadcastReceiver沒有監聽手機狀態 - 當應用程序未被使用時遺憾地停止
當我使用應用程序時,這個場景工作正常。但如果我關閉應用程序,然後得到Unfortunately stopped
這是我的AndroidManifest.xml。
<receiver android:name="com.domyhome.broadcastreceiver.IncomingCallInterceptor" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
這是我的onRecive方法。當我使用我的應用程序時如果我接到一個電話,那麼當前活動不會進入背景。所以我寫了代碼來完成活動。
@Override
public void onReceive(Context context, Intent intent) {
LoginActivity.login.finish();
MainActivity.main.finish();
telephonyRegister(context,intent);
}
這是功能。當電話狀態爲振鈴時,我撥打ProjectDailogActivity
。當電話出席或結束時,我正在完成同一活動。
public void telephonyRegister(final Context context, Intent intent)
{
TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
ctx=context;
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_IDLE)) {
try
{
ProjectDailogActivity.projdialog.finish();
}catch(Exception e)
{
e.printStackTrace();
}
if(ring==true&&callReceived==false)
{
Toast.makeText(context, "THIS IS MISSED CALL FROM"+phoneNumber, Toast.LENGTH_LONG).show();
String smsmessage = "Thanks for contacting us. We will get back you soon";
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber, null, "Dear Customer!"+" "+smsmessage, null, null);
Log.i("sms",smsmessage);
Toast.makeText(context, "SMS sent.",
Toast.LENGTH_LONG).show();
callReceived=false;
}
else if(callReceived==true)
{
try
{
ProjectDailogActivity.projdialog.finish();
}catch(Exception e)
{
e.printStackTrace();
}
if(Conntact.isAvailable(phoneNumber))
{
try{
myAudioRecorder.release();
myAudioRecorder.stop();
myAudioRecorder = null;
}
catch(Exception e)
{
System.out.print("error");
}
Toast.makeText(context,"Recording Stopped", Toast.LENGTH_SHORT).show();
}
}
}
else if(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_OFFHOOK))
{
try{
ProjectDailogActivity.projdialog.finish();
}catch(Exception e)
{
e.printStackTrace();
}
dir = Environment.getExternalStorageDirectory();
try {
file = File.createTempFile("sound", ".mp3", dir);
} catch (IOException e) {
e.printStackTrace();
return;
}
myAudioRecorder = new MediaRecorder();
myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
myAudioRecorder.setOutputFile(file.getAbsolutePath());
if(Conntact.isAvailable(phoneNumber))
{
callReceived = true;
ring=false;
try {
myAudioRecorder.prepare();
myAudioRecorder.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Toast.makeText(context, "recording started", Toast.LENGTH_SHORT).show();
}
else
{
}
}
else if(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_RINGING)){
phoneNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
ring = true;
System.out.println("phonenum"+Conntact.isAvailable(phoneNumber));
if(!Conntact.isAvailable(phoneNumber))
{
Toast.makeText(ctx, phoneNumber+"not available", Toast.LENGTH_SHORT).show();
}
else if(Conntact.isAvailable(phoneNumber))
{
Intent intent11 = new Intent(ctx,ProjectDailogActivity.class);
intent11.putExtra("incoming_number",phoneNumber);
intent11.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(intent11);
}
}
}
Conntact
是已經存儲了一些數字的表格。因此,當我接到電話時,只需檢查Conntact
表中的主叫號碼是否可用。
如果可用,則顯示ProjectDailogActivity
。如果號碼可用並且通話也被選中,則意味着要進行錄音。一切工作正常。但是當我的應用沒有被使用時,它顯示Unfortunately stopped
。
總是發表logcat的應用程序時墜毀是 – 2014-12-05 08:40:17
@MysticMagic我從膩子的電話。當我連接我的仿真器時,設備將斷開連接。我如何獲得Logcat的詳細信息? – user3764346 2014-12-05 08:42:00