我想在我的應用程序中偵聽傳入呼叫,所以我創建了一個相同的廣播接收器,但是當我在傳遞中傳遞上下文時,它顯示一個錯誤。任何人都可以判斷我做錯了什麼? 這裏是我的代碼:在廣播接收機中的上下文android
public class MainActivity extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
TelephonyManager tmngr= (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
MyPhoneStateListener PhoneListener = new MyPhoneStateListener();
tmngr.listen(PhoneListener,PhoneStateListener.LISTEN_CALL_STATE);
}
private class MyPhoneStateListener extends PhoneStateListener {
public void onCallStateChanged(int state,String incoming)
{
if (state == 1) {
String msg = "New Phone Call Event. Incomming Number : "+incoming;
int duration = Toast.LENGTH_LONG;
//i am getting error here(context)
Toast toast = Toast.makeText(context, msg, duration);
toast.show();
}
}}}
你在哪裏初始化pcontext?你會得到什麼錯誤? – Egor 2015-04-03 22:33:00
多數民衆贊成多數民衆黨我想知道我應該使一個本地上下文名稱作爲pcontext – user3570390 2015-04-03 22:35:31
你有onReceive()中的上下文的引用,只是通過它的構造函數傳遞給MyPhoneStateListener並使用它。 – Egor 2015-04-03 22:36:49