2012-06-22 3 views
1

내가 OnResume 내 reciver입니다을 등록하고 있고 OnPause에 뭔가를한다 등록 취소는 매니페스트 파일수없는 동적으로

package com.bd2; 
import android.app.Activity; 
import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.os.Bundle; 
import android.provider.Contacts.People; 
import android.util.Log; 

public class BroadcastReceiver2Activity extends Activity { 
/** Called when the activity is first created. */ 
private NotificationManager mNotificationManager; 
private int SIMPLE_NOTFICATION_ID; 
IntentFilter intentfilter; 



    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    intentfilter = new IntentFilter(); 
    intentfilter.addAction("android.intent.action.AIRPLANE_MODE"); 


} 

@Override 
    protected void onResume() { 
    // TODO Auto-generated method stub 
    super.onResume(); 
    /*intfilter = new IntentFilter(); 
     intfilter.addAction("android.intent.action.AIRPLANE_MODE");*/ 
    registerReceiver(receiver, intentfilter); 
    //  sendBroadcast(); 

    } 

    @Override 
    protected void onPause() { 
    // TODO Auto-generated method stub 
    super.onPause(); 
    unregisterReceiver(receiver); 


    } 

    private BroadcastReceiver receiver=new BroadcastReceiver(){ 


    @Override 
    public void onReceive(Context context, Intent intent) { 
     // TODO Auto-generated method stub 
     mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); 
      Notification notifyDetails = new Notification(R.drawable.icon,"Time Reset!",System.currentTimeMillis()); 
      PendingIntent myIntent = PendingIntent.getActivity(context, 0, new Intent(Intent.ACTION_VIEW, People.CONTENT_URI), 0); 
      notifyDetails.setLatestEventInfo(context, "Time has been Reset", "Click on me to view Contacts", myIntent); 
      notifyDetails.flags |= Notification.FLAG_AUTO_CANCEL; 
      mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails); 
       Log.i(getClass().getSimpleName(),"Sucessfully Changed Time"); 
    } 

    }; 



    } 

//////// 내 코드에 문제가 있습니다

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.bd2" 
    android:versionCode="1" 
    android:versionName="1.0"> 
<uses-sdk android:minSdkVersion="8" /> 

<application android:icon="@drawable/icon" android:label="@string/app_name"> 
    <activity android:name=".BroadcastReceiver2Activity" 
       android:label="@string/app_name"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 

    </activity> 



</application> 

내 알림을 받고 있지 않다. 내가 정적이 일을하고있어 경우 그것은 작동

+0

이것은 다른 사용자들과 마찬가지로 매우 좋은 질문입니다. 나중에이 시나리오를 실행할 것입니다. 동적으로 방송 수신기를 등록 :) .. : D – t0mm13b

+0

t0mm13b @ 동적으로 방송 수신기를 등록하는 것은 쉽고, 비키 바로 그것을하고있다. 당신이 활동을 일시 중지해야 (수신기를 등록 취소)/오프에 비행기 모드를 설정하기 위해 : 문제는 어제 내 대답에서 지적한 것입니다. 따라서 방송을 놓친다. 이것은 동적으로 등록해서는 안되는 수신기의 예입니다. –

답변

0

편집 # 3 : onResume 핸들러에서

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    mIntentFilter.addAction("android.intent.action.SERVICE_STATE"); 
    mIntentFilter.addAction("android.intent.action.AIRPLANE_MODE"); 
    registerReceiver(receiver, mIntentfilter); 
} 

: 다음에서 onCreate 핸들러에서

private IntentFilter mIntentFilter; 

: 글로벌 변수를 정의

onPause 헥타르에서
@Override 
protected void onResume() { 
    // TODO Auto-generated method stub 
    super.onResume(); 
    // Be careful here... mIntentFilter might be gc'd! add checking to this! 
    registerReceiver(receiver, mIntentFilter); 
    Log.d(TAG, "onResume() - Registered!"); 
} 

ndler : 방송 수신기에서

@Override 
protected void onPause() { 
    // TODO Auto-generated method stub 
    unregisterReceiver(receiver); 
    Log.d(TAG, "onPause() - Unregistered!"); 
    super.onPause(); 
} 

: 우리가 프로그래밍 방식을하고있는이 시점에서

private BroadcastReceiver receiver=new BroadcastReceiver(){ 
@Override 
    public void onReceive(Context context, Intent intent) { 
     // TODO Auto-generated method stub 
     if (intent != null && intent.getAction().equals(Intent.ACTION_AIRPLANE_MODE_CHANGED)){ 
      Log.d(TAG, "receiver/onReceive() - GOT THE INTENT!!!!"); 
     }else{ 
      Log.d(TAG, "receiver/onReceive() - NOPE! NO INTENT!"); 
     } 
     mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); 
      Notification notifyDetails = new Notification(R.drawable.icon,"Time Reset!",System.currentTimeMillis()); 
      PendingIntent myIntent = PendingIntent.getActivity(context, 0, new Intent(Intent.ACTION_VIEW, People.CONTENT_URI), 0); 
      notifyDetails.setLatestEventInfo(context, "Time has been Reset", "Click on me to view Contacts", myIntent); 
      notifyDetails.flags |= Notification.FLAG_AUTO_CANCEL; 
      mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails); 
       Log.i(getClass().getSimpleName(),"Sucessfully Changed Time"); 
    } 

    }; 

는 매니페스트에서 (앞서 언급 한) 의도 수신기를 제거합니다. 또한 권한을 정의하십시오!

<uses-permission android:name="android.permission.READ_PHONE_STATE"/> 
+0

... 그것은 –

+0

당신이 대신 동적 런타임에 등록되는 방송 수신기를 포함하는 클래스를 생성 봤어 충돌 브로 작동하지? – t0mm13b

+0

나중에 나는 그것을 만들었지 만 동적 인 방법으로 도움이 될 수있는이 방법으로 필요합니다! –

0

모든 것은 당신이 당신의 수신기의 등록을 취소 귀하의 활동을 일시 중지없이 또는 해제 비행기 모드를 켜 수 없다는 사실이 아닌 나에게 좋아 보인다. 활동으로 돌아가서 다시 시작하고 다시 등록 할 때까지 방송을 놓쳤습니다.

나는 당신의 궁극적 인 목표는 여기에 무엇인지 모른다, 그러나 당신이 당신의 앱이 비행기 모드 의도를 수신 할 경우, 가장 좋은 아이디어는 매니페스트에 등록하는 것입니다. (기술적으로는 동적으로 할 수는 있지만, 실제로는 사용자가 원하는 것 같지 않은/onPause()보다 더 영구적 인 등록/등록 취소가 필요합니다.

+0

안녕하세요 Darshan 내가 staic에서 acheived하지만 나는 그것을 할 동적으로 할 수있는 내가 뭘해야 말해 줄 수 –

+0

'onCreate()'에 등록하고'onDestroy()'에서 등록을 취소 할 수 있으며, 작업이 언제든지 파괴 될 수 있다는 점을 제외하고는 작업이 중단됩니다. 그것은 나쁜 생각이고 당신은 그것을해서는 안됩니다. Activity가 실행 중인지 여부에 상관없이 시스템이 실제로 BroadcastReceiver를 인스턴스화하게하려면 (거의 확실하게), Manifest에 수신기를 선언해야합니다. –

+0

내가 onCreate()에 등록하고 onDestory()에서 등록을 취소하지만 onDestroy() 메서드에서 can not는 super.onPause()를 호출합니다. –