2011-12-27 7 views
1

を照会しようとすると、私はBroadcastReceiver内でこのコードを持っている:は、私が得た以前の回答に基づいてContactList

Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(ASenderTel)); 
// Also tried; 
//ContentResolver cr = getContentResolver(); 
//Context c = this; 
Cursor c = getContentResolver().query(lookupUri, new String[] { PhoneLookup._ID }, null, null, null); 
return (c.getCount() > 0); 

を...しかし、ERRのMSGを取得し、「方法getContentResolverは()のタイプのために定義されていませんKITSMSReceiver "

+0

私が使用している場合:。 ContentResolverのCR = getContentResolver()クエリ(lookupUri、 新しいString [] {PhoneLookup._ID} 、null、null、null); ... ERRのMSGは、ある "方法getContentResolver()型KITSMSReceiverため未定義である" (KITSMSReceiverがBroadcastReceiverある)ひどく入り組んだと思わ –

答えて

2

getContentResolver()はandroid.content.Contextクラスのメソッドです。たとえば、あなたの活動からそれにアクセスすることができます。それはアクティビティクラス内放送受信機を置くachiveするには、次の

迅速なドラフト:

public class MyActivity extends Activity { 

    // ... 

    private BroadcastReceiver myReceiver = new BroadcastReceiver() { 
    public void onReceive(Context c, Intent i) { 
     MyActivity.this.recvBroadcast(i); // forward to your activity 
     MyActivity.this.getContentResolver(); // <----- 
    } 
    }}; 

    // ... 

} 
+0

。それは本当に「好ましい方法」ですか?プライベートBroadcastReceiverは、イベントを呼び出す必要がありますか? –

+0

実際には、onReceiveメソッドに付属しているContextパラメータを使用して、BroadcastReceiverを別のファイルに置くことができます – marcinj