2016-11-10 11 views
-1

여기서는 sms가 수신되고 수신 전화가 수신되면 플래시를 시작하는 응용 프로그램을 만듭니다. 플래시가 SMS 수신 중이나 통화 중이 아닌 이유는 무엇입니까? 누구든지 도와 줄 수 있습니까? 나는이 일을 여러 날 동안 붙들고있다. 미리 감사드립니다, 내 코드는 아래에 주어집니다. 플래시는 SMS 수신 만 수행하지만 호출 수신은 수행하지 않습니다. 당신의 인도를 기다리고 있습니다. 이것에 관해서 검색했을 때 클래스와 메소드만으로 내 애플리케이션을 만들었습니다. 설명을 위해 요청하는수신 전화 감지

public class SMSReceiver extends BroadcastReceiver { 
     Boolean call; 
     private Camera camera; 
     int count; 
     long delaytime; 
     Editor editor; 
     String flashtype; 
     private boolean hasFlash; 
     private boolean isFlashOn; 
     private boolean isFlashblinking; 
     private boolean mActive; 
     private Handler mHander; 
     private final Runnable mRunnable; 
     private boolean mSwap; 
     private Context mcontext; 
     AudioManager myAudioManager; 
     String noofblinks; 
     int numbofblink; 
     Parameters params; 
     SharedPreferences pref; 
     StringBuilder result; 
     Boolean ring; 
     Boolean silent; 
     Boolean sms; 
     String timetoblink; 
     Boolean vibrate; 

     public class PhoneListener extends PhoneStateListener { 
      private Context context; 

      public PhoneListener(Context c) { 
       Log.i("CallRecorder", "PhoneListener constructor"); 
       this.context = c; 
      } 

      public void onCallStateChanged(int state, String incomingNumber) { 
       switch (state) { 
       case 0: 
        try { 
         SMSReceiver.this.mHander 
           .removeCallbacks(SMSReceiver.this.mRunnable); 
         if (SMSReceiver.this.camera != null) { 
          SMSReceiver.this.camera.release(); 
         } 
        } catch (Exception e) { 
         try { 
          SMSReceiver.this.camera.release(); 
         } catch (Exception e2) { 
         } 
        } 
       case 1: 
        try { 
         if (SMSReceiver.this.call.booleanValue()) { 
          if (SMSReceiver.this.myAudioManager.getRingerMode() == 0 
            && SMSReceiver.this.silent.booleanValue()) { 
           SMSReceiver.this.flash(); 
          } 
          if (SMSReceiver.this.myAudioManager.getRingerMode() == 1 
            && SMSReceiver.this.vibrate.booleanValue()) { 
           SMSReceiver.this.flash(); 
          } 
          if (SMSReceiver.this.myAudioManager.getRingerMode() == 2 
            && SMSReceiver.this.ring.booleanValue()) { 
           SMSReceiver.this.flash(); 
          } 
         } 
        } catch (Exception e3) { 
        } 
       case 2: 
        try { 
         SMSReceiver.this.mHander 
           .removeCallbacks(SMSReceiver.this.mRunnable); 
         if (SMSReceiver.this.camera != null) { 
          SMSReceiver.this.camera.release(); 
         } 
        } catch (Exception e4) { 
         try { 
          SMSReceiver.this.camera.release(); 
         } catch (Exception e5) { 
         } 
        } 
       default: 
       } 
      } 
     } 

     public SMSReceiver() { 
      this.mHander = new Handler(); 
      this.mActive = false; 
      this.mSwap = true; 
      this.isFlashblinking = true; 
      this.count = 0; 
      this.mRunnable = new Runnable() { 

       @Override 
        // TODO Auto-generated method stub 
        public void run() { 
         try { 
          SMSReceiver sMSReceiver = SMSReceiver.this; 
          sMSReceiver.count++; 
         } catch (Exception e) { 
         } 
         if (SMSReceiver.this.mActive) { 
          if (SMSReceiver.this.count >= SMSReceiver.this.numbofblink * 2) { 
           try { 
            SMSReceiver.this.mHander 
              .removeCallbacks(SMSReceiver.this.mRunnable); 
            SMSReceiver.this.camera.release(); 
           } catch (Exception e2) { 
           } 
          } 
          if (SMSReceiver.this.isFlashOn) { 
           SMSReceiver.this.turnOffFlash(); 
          } else { 
           SMSReceiver.this.turnOnFlash(); 
          } 
          try { 
           SMSReceiver.this.mHander.postDelayed(
             SMSReceiver.this.mRunnable, 
             SMSReceiver.this.delaytime); 
          } catch (Exception e3) { 
          } 
         } 
        } 
      }; 
     } 

     public void onReceive(Context context, Intent intent) { 
      try { 
       this.mcontext = context; 
       this.count = 0; 
       try { 
        this.pref = PreferenceManager 
          .getDefaultSharedPreferences(this.mcontext); 
        this.editor = this.pref.edit(); 
        this.call = Boolean.valueOf(this.pref.getBoolean("call", true)); 
        this.sms = Boolean.valueOf(this.pref.getBoolean("sms", true)); 
        this.timetoblink = this.pref.getString("blinktime", "200"); 
        this.noofblinks = this.pref.getString("noofblinks", "5"); 
        this.ring = Boolean.valueOf(this.pref.getBoolean("ring", true)); 
        this.vibrate = Boolean.valueOf(this.pref.getBoolean("vibrate", 
          true)); 
        this.silent = Boolean.valueOf(this.pref.getBoolean("silent", 
          true)); 
        this.flashtype = this.pref.getString("flashtype", "1"); 
        this.delaytime = Long.parseLong(this.timetoblink); 
        this.numbofblink = Integer.parseInt(this.noofblinks); 
        this.myAudioManager = (AudioManager) this.mcontext 
          .getSystemService("audio"); 
       } catch (Exception e) { 
       } 
       ((TelephonyManager) this.mcontext.getSystemService("phone")) 
         .listen(new PhoneListener(context), 32); 
      } catch (Exception e2) { 
      } 
     } 

     public void flash() { 
      try { 
       this.hasFlash = this.mcontext.getPackageManager().hasSystemFeature(
         "android.hardware.camera.flash"); 
       if (this.hasFlash) { 
        getCamera(); 
        startStrobe(); 
        return; 
       } 
       AlertDialog alert = new Builder(this.mcontext).create(); 
       alert.setTitle("Error"); 
       alert.setMessage("Sorry, your device doesn't support flash light!"); 

       alert.setButton("OK", new OnClickListener() { 

        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         // TODO Auto-generated method stub 

        } 
       }); 
       alert.show(); 
      } catch (Exception e) { 
      } 
     } 

     private void getCamera() { 
      if (this.camera == null) { 
       try { 
        this.camera = Camera.open(); 
        this.params = this.camera.getParameters(); 
       } catch (Exception e) { 
        Log.e("Camera Error. Failed to Open. Error: ", e.getMessage()); 
       } 
      } 
     } 

     private void turnOnFlash() { 
      try { 
       if (!this.isFlashOn && this.camera != null && this.params != null) { 
        this.params = this.camera.getParameters(); 
        if (this.flashtype.equals("2")) { 
         this.params.setFlashMode("torch"); 
        } else if (this.flashtype.equals("3")) { 
         this.params.setFlashMode("torch"); 
        } else { 
         this.params.setFlashMode("torch"); 
        } 
        this.camera.setParameters(this.params); 
        this.camera.startPreview(); 
        this.isFlashOn = true; 
       } 
      } catch (Exception e) { 
      } 
     } 

     private void turnOffFlash() { 
      try { 
       if (this.isFlashOn && this.camera != null && this.params != null) { 
        this.params = this.camera.getParameters(); 
        this.params.setFlashMode("off"); 
        this.camera.setParameters(this.params); 
        this.camera.stopPreview(); 
        this.isFlashOn = false; 
       } 
      } catch (Exception e) { 
      } 
     } 

     private void startStrobe() { 
      try { 
       this.mActive = true; 
       this.mHander.post(this.mRunnable); 
      } catch (Exception e) { 
      } 
     } 
    } 

답변

0

가에서 그 코드를 작성 걸려 오는 전화

에 대해 알고 방송 수신기를 확인 같은

http://androidexample.com/Incomming_Phone_Call_Broadcast_Receiver__-_Android_Example/index.php?view=article_discription&aid=61

+0

내 코드를 확인 했습니까? 내 코드를 변경하고 싶습니다 .. 내 코드의 오류를 추적 할 수 있습니까? –

+0

음, 상태가 1 일 때 토스트를 추가하고 수신 전화에 나타나는지 확인해보십시오. 일단 당신이 당신이 틀릴지도 모른다는 단서가 있다면 행복 코딩 –

+0

네, 시도해 보도록하겠습니다. 해결책이 있다면 –

0

이 링크를 따라 행할 AndroidManifes.xml

<receiver android:name=".ServiceReceiver" > 
    <intent-filter> 
     <action android:name="android.intent.action.PHONE_STATE" /> 
    </intent-filter> 
</receiver> 

그런 새로운 수업을 만드십시오.

public class ServiceReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(final Context context, Intent intent) { 
     TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); 
     telephony.listen(new PhoneStateListener(){ 
      @Override 
      public void onCallStateChanged(int state, String incomingNumber)   { 
     if(state.equals(TelephonyManager.EXTRA_STATE_RINGING)){ 
    // Run the flash in that line 
     } 

      } 
     },PhoneStateListener.LISTEN_CALL_STATE); 
    } 
} 
+0

또한 Read.phone의 사용 권한을 사용하십시오. androidManifest.xml에있는 상태 –

+0

원시 유형 int에 equals (String)를 호출 할 수 없습니다. –

+0

죄송합니다. IDE없이 해당 코드를 작성합니다. –