1

Android 애플리케이션에서 수신되는 SMS를 모두 읽으려고합니다. 필자는 브로드 캐스트 리시버를 작성하여 메시지를 읽고 안드로이드 매니페스트에 사용 권한을 추가했습니다. 다음 오류가 발생합니다 :Android 브로드 캐스트 수신기가 터지지 않음

android.content.ActivityNotFoundException : 명시 적 활동 클래스 {com.example.asus.otpclippr/com.example.asus.otpclippr.readerService}을 찾을 수 없습니다. AndroidManifest.xml에서이 활동을 선언하셨습니까?

이것은 AndroidManifest.xml을

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.asus.otpclippr"> 
<uses-permission-sdk-23 android:name="android.permission.BROADCAST_SMS"/> 
<uses-permission-sdk-23 android:name="android.permission.RECEIVE_SMS" /> 
<uses-permission-sdk-23 android:name="android.permission.SEND_SMS" /> 
<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity android:name=".MainActivity"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <receiver android:name="com.example.asus.otpclippr.readerService" > 
     <intent-filter> 
      <action android:name="android.provider.Telephony.SMS_RECEIVED"  /> 
     </intent-filter> 
    </receiver> 
</application> 

이것은 MainActivity.java 패키지 com.example.asus.otpclippr이다;

import android.app.ActivityManager; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.pm.PackageManager; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.CheckBox; 

public class MainActivity extends AppCompatActivity { 
    CheckBox copyClip, createNotif ; 
    Button btn ; 
    private BroadcastReceiver br ; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    copyClip = (CheckBox)findViewById(R.id.checkBox) ; 
    createNotif = (CheckBox)findViewById(R.id.checkBox2) ; 
    btn = (Button)findViewById(R.id.button) ; 
    btn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      boolean res = false ; 
      res = isMyServiceRunning(readerService.class) ; 
      Intent intent = new  Intent(MainActivity.this,readerService.class) ; 
      //intent.setAction("android.provider.Telephony.SMS_RECEIVED") ; 
      intent.putExtra("flag1",copyClip.isChecked()); 
      intent.putExtra("flag2",createNotif.isChecked()) ; 
      Log.d("Checkpoint1","calling broadcast") ; 
      startActivity(intent); 
     } 
    }); 

} 

/*public boolean isMyServiceRunning(Class<?> serviceClass){ 
    ActivityManager activityManager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE) ; 
    for(ActivityManager.RunningServiceInfo service : activityManager.getRunningServices(Integer.MAX_VALUE)){ 
     if(serviceClass.getName().equals(service.service.getClassName())){ 
      return true ; 
     } 
    } 
    return false ; 
    }*/ 
} 

그리고 이것은 브로드 캐스트 리시버 패키지 com.example.asus.otpclippr이다; ? 당신은 Intent으로 startActivity()를 호출

 Intent intent = new  Intent(MainActivity.this,readerService.class) ; 
     //intent.setAction("android.provider.Telephony.SMS_RECEIVED") ; 
     intent.putExtra("flag1",copyClip.isChecked()); 
     intent.putExtra("flag2",createNotif.isChecked()) ; 
     Log.d("Checkpoint1","calling broadcast") ; 
     startActivity(intent); 

을 :

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Build; 
import android.os.Bundle; 
import android.provider.Telephony; 
import android.telephony.SmsManager; 
import android.telephony.SmsMessage; 
import android.util.Log; 
import android.widget.Toast; 

/** 
* Created by ASUS on 27-12-2016. 
*/ 

public class readerService extends BroadcastReceiver { 
    SmsManager smsManager = SmsManager.getDefault() ; 
    String[] msg ; 
    @Override 
    public void onReceive(Context context, Intent intent) { 



    final Bundle bundle = intent.getExtras() ; 
    try{ 
     if(bundle!=null) { 
      SmsMessage smsMessage; 

      if (Build.VERSION.SDK_INT >= 19) { //KITKAT 
       SmsMessage[] msgs = Telephony.Sms.Intents.getMessagesFromIntent(intent); 
       smsMessage = msgs[0]; 
      } else { 
       Object pdus[] = (Object[]) bundle.get("pdus"); 
       smsMessage = SmsMessage.createFromPdu((byte[]) pdus[0]); 
      } 
      String message = smsMessage.getMessageBody() ; 
      String[] checks = new String[]{"OTP","Transaction","Password","key","card","txn"} ; 
      msg = message.split(" ") ; 
      Log.d("Message",message) ; 
      for(int i=0;i<checks.length;i++){ 
       if(message.contains(checks[i])){ 
        checkforOTP(context,message) ; 
        Log.d("Check2","Checking for OTP") ; 
        break; 
       } 
      } 

      /*final Object[] pdusObj = (Object[]) bundle.get("pdus"); 
      for (int i = 0; i < pdusObj.length; i++) { 

       SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]); 
       String phoneNumber = currentMessage.getDisplayOriginatingAddress(); 

       String senderNum = phoneNumber; 
       String message = currentMessage.getDisplayMessageBody(); 

       Log.d("SmsReceiver", "senderNum: " + senderNum + "; message: " + message); 
       String[] msg = message.split(" ") ; 
       Toast.makeText(context,message,Toast.LENGTH_SHORT).show(); 
       String[] checks = new String[] {"OTP","Transaction","Password","key","card"} ; 
       for(int j=0; j< checks.length; j++){ 
        if(message.contains(checks[j])){ 
         checkforOTP(message) ; 
         break; 
        } 
       } 
      }*/ 
     } 
    }catch (Exception e){ 
     e.printStackTrace(); 
    } 
} 

public void checkforOTP(Context context ,String message){ 
    int pos = message.indexOf("is") ; 
    String msg1 = message.substring(pos,message.length()) ; 
    String otp1 = msg1.split(" ")[0]; 
    String msg2 = message.substring(0,pos) ; 
    String[] tempA = msg2.split(" ") ; 
    String otp2 = tempA[tempA.length -1] ; 
    if(isValid(otp1)){ 
     Toast.makeText(context,"OTP is"+otp1,Toast.LENGTH_SHORT).show(); 
    } 
    else if(isValid(otp2)){ 
     Toast.makeText(context,"OTP is "+otp2,Toast.LENGTH_SHORT).show(); 
    } 
    else{ 
     Log.d("check3","Wrong call") ; 
    } 

} 

public boolean isValid(String x){ 
    if(!(x.length()==4 || x.length() ==6)){ 
     return false ; 
    } 
    if(x.matches("\\d+")){ 
     return true ; 
    } 
    return false ; 
} 
} 

MainActivity에서 방송 수신기를 호출하는 당신이 OnClickListener에서이 일을하는

답변

0

미리 감사를 올바른 방법은 무엇입니까 이는 BroadcastReceiver을 타겟팅합니다. readerServiceActivity이 아니라 BroadcastReceiver입니다. 이것은 작동하지 않습니다.

+0

감사합니다. onClickListener가 브로드 캐스트 수신기를 트리거 할 수 있도록 몇 가지 변경 사항을 제안 할 수 있습니까? 미리 감사드립니다. – user6739649

+0

죄송합니다, 이해가 안됩니다. 들어오는 SMS가 도착하면 'BroadcastReceiver'가 실행됩니다. 그게 네가 원하는거야? –

+0

예. 수신 SMS를 읽고 토스트 메시지를 작성하려고합니다. 아무 것도 변경할 필요가 없다. 나는 방송 수신기에 어떤 종류의 호출이 있어야한다고 생각했다. – user6739649

관련 문제