因此,在我的Android应用中,我一直在测试如何检测来电和去电。我通过构建扩展BroadcastReceiver的类来使其工作,但是在该类中,如果我调用另一个类,则会崩溃。例如MyClass mc = new MyClass(); mc.functionname();
我的实际应用程序正在运行一个循环的声音剪辑。我想在拨入或拨出电话时暂停该声音片段。声音片段在扩展Activity的类中播放。我该怎么做呢?
这就是我的BroadcastReceiver类和清单。
package com.anthony.phone;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;
public class inComingReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Bundle bundle = intent.getExtras();
if(null == bundle)
return;
Log.i("MYTAG",bundle.toString());
String state = bundle.getString(TelephonyManager.EXTRA_STATE);
Log.i("MYTAG","State: "+ state);
if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING))
{
String phonenumber = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.i("MYTAG",":) Incomng Number: " + phonenumber);
String info = "Detect Calls sample application\nIncoming number: " + phonenumber;
Toast.makeText(context, info, Toast.LENGTH_LONG).show();
}
}
}
和我的清单
<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.anthony.phone"
android:versionCode="1"
android:versionName="1.0" xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".CallTActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.anthony.phone.inComingReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
</application>
</manifest>
最佳答案
我也有这种问题。我通过从广播接收器调用活动并在特定的活动中编写我的班级代码来解决。如下所述
Intent i = new Intent(context,newActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
我希望它将帮助您解决您的查询。
问我您是否还有任何疑问。