我有一個Java類與佈局。我想讓BroadcastReceiver獲得來電號碼。我怎麼能在我的活動來實現這個目的:接收和註冊BroadcastReceiver
此,我想這一點:
package com.example.callchecker;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.content.BroadcastReceiver;
import android.content.Intent;;
public class Mark_number extends Activity {
Context context = this ;
Dialog D =new Dialog(context);
Button btn_dialog = (Button) findViewById(R.id.btn_dialog);
Button btn_dismiss = (Button) D.findViewById(R.id.btn_dismiss);
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.mark_number);
private BroadcastReceiver myR = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
}
};
}
}
這是正確的做法還是我不得不做出這樣的擴展BraodcastRceiver另一個類?
public class My_Receiver extends BroadcastRceiver {
如果要創建另一個類,那麼如何從我的主要活動調用它?
hi natiq:如果您沒有要求動態刪除收聽,您可以在清單中註冊接收器。 –