BroadcastReceiver 를 상속 받은 class = A.class 가 있고 Main 액티비티가 있습니다.
제가 하려는 부분이 A.class 에서 Main 액티비티에다가 브로드캐스트를 날리면 Main 에서 onReceive(){} 에서 임의의 메서드를 실행 하려고 하는 데요...
이때 굼긍한 점이 ... Main 에다가 BroadcastReceiver 의 객체를 선언을 다 했구 이제 A.class 에서 브로드캐스트를 날리기만 하면 되는데 안되 더라구요..
Intent intent = new Intent("start_push_service");
startService(intent);
혹은
Intent intent = new Intent("start_push_service");
sendBroadcast(intent);
이렇게 하면은 startService() , sendBroadcast() 둘 다 정의가 안되 있다구 나오더라구요...
보니깐 둘다 android.content.ContextWrapper에 있는 메서드 들이길래 import android.content.*; 이렇게 해주어도 똑같이 정의가 안되었다구 나오더라구요........
답변좀 부탁드리겠습니다.......................
리시버 클래스가 A인지 메인인지 좀 햇갈리네요....
암튼 리시버 클래스에서 startService 나 sendBroadcast 하시려는 게 맞는지 모르겠는데
맞다면 두 메서드는 리시버에 포함된 메서드가 아니라서
context.startService 하셔야되요
리시버 클래스는 메인이요.. A에서 날리는 거구요...
그리구 말씀 하신것처럼 context.startService 하니깐 되더라구요...... 문제가요... 제가 Main 에서 BroadcastReceiver 를 인스턴스를 생성해서 OnReceive(){} 를 사용하고 있는데요...
public class Main extends Activity {
private BroadcastReceiver receiver ;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
IntentFilter filter = new IntentFilter();
filter.addAction("start_push_service");
receiver = new BroadcastReceiver(){
@Override public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if ("start_push_service".equals(action)){
System.out.println("this is broadcastReceiver");
}
}
protected void onDestroy(){
unregisterReceiver(receiver);
}
};
registerReceiver(receiver,filter);
이렇게 했는데요..... 안되더라구여........ Manifest 에는 아무것도 등록을 안했구요.... 혹시 어디가 잘못 됬는지 알수 있을까요...?
BroadcastReceiver 를 등록했나요?
즉 registerReceiver() 하시면 잘 될거 같은데요..