2011-12-30 3 views
1

SMS 용 응용 프로그램을 개발 중이며 MessageURI를 SMS 용 데이터로 전달하는 경우 "보낸 사람"의도를 수신 할 수 없습니다. 예외가 발생하고 SMS는 대기 상태에 있습니다. OnReceive가 호출되지 않았습니다 !!!SMS 수신자가 의도를 수신하지 못했습니다.

public class Sms_SendActivity extends Activity { 
    PendingIntent sentPI; 
    String Sent = "SENT_SMS"; 
    BroadcastReceiver br; 
    Button btnSend; 
    Context mcontext; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     Uri uri = Uri.parse("Content://sms/2"); 
     mcontext = getApplicationContext(); 
     sentPI = PendingIntent.getBroadcast(this,0,new Intent(Sent,uri),0); 
     btnSend = (Button)findViewById(R.id.send); 
br = new BroadcastReceiver() { 

      @Override 
      public void onReceive(Context context, Intent intent) { 
       Log.w("Check","Inside On Receiver"); 
       switch (getResultCode()) { 
       case Activity.RESULT_OK: 
        Toast.makeText(mcontext,"Inside Sms sent", Toast.LENGTH_SHORT).show(); 
        Log.w("Check"," URI "+intent.getData().toString()); 
        break; 

       default: 
        Toast.makeText(mcontext,"failure", Toast.LENGTH_SHORT).show(); 
        break; 
       } 
       unregisterReceiver(br); 
      } 
     }; 

     btnSend.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       SmsManager sms = SmsManager.getDefault(); 
       sms.sendTextMessage("1-212-555-1212", null, "Hi there", sentPI, null); 
       Log.w("Check","Sms Queued"); 
       try { 
        registerReceiver(br, new IntentFilter(Sent,"content://sms")); 
       } catch (MalformedMimeTypeException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 

      } 
     }); 

    } 
} 

나를 도와 줄 수 있습니까?

감사합니다.

+0

나는 비슷한 것을하고있다 !!! http://stackoverflow.com/questions/14571564/android-pendingintent-extras-not-received-by-broadcastreceiver/14612215#14612215 – toobsco42

답변

0

코드가 약간 수정되었습니다. 내 경우에는 그것을 작동 :

public class DynBroadCastSMSActivity extends Activity { 


private static final String TAG = DynBroadCastSMSActivity.class.getSimpleName(); 

PendingIntent sentPI; 
String Sent = "SMS_SENT"; 
Button btnSend; 
Context mContext; 

BroadcastReceiver br = new BroadcastReceiver() { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     Log.w("Check", "Inside On Receiver"); 
    } 
}; 

@Override 
protected void onResume() { 
    super.onResume(); 
    IntentFilter filter = new IntentFilter(); 
    filter.addAction(Sent); 
    filter.addDataScheme("mycontent"); 
    registerReceiver(br, filter); 
} 

@Override 
protected void onPause() { 
    super.onPause(); 
    unregisterReceiver(br); 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    Uri uri = Uri.parse("mycontent://sms"); 
    Intent intent = new Intent(Sent); 
    intent.setData(uri); 
    mContext = getApplicationContext(); 
    sentPI = PendingIntent.getBroadcast(this, 0, intent, 0); 
    btnSend = (Button) findViewById(R.id.send); 



    btnSend.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      SmsManager sms = SmsManager.getDefault(); 
      sms.sendTextMessage("5554", null, "Hi there", sentPI, 
        null); 
      Log.w("Check", "Sms Queued"); 
     } 
    }); 

} 

}

문제는 다음이었다. Android는 '콘텐츠'라는 데이터 스키마를 인식하지 못합니다. 내가 "내 내용"이라 부르면 모든 것이 작동합니다. 이 코드를 사용하여 더 자세한 정보를 얻을 수 있습니다.

+0

감사합니다. @ 유료 괜찮습니다.하지만 SMS에 액세스하고 있습니다. uri는 "content : // sms /"형식입니다.하지만 "mycontent"를 "content"로 바꿀 경우 .OnReceive가 호출되지 않습니다. 명확히 할 수 있습니까? 미리 감사드립니다 – siva

+0

나는 이것을 설명하는 방법을 모른다. 다른 경우에는 데이터 유형을 지정해야한다고 생각합니다. 그러나 정확한 대답을 찾아보십시오. – Yury

+0

감사합니다. @Yury !!! – siva

관련 문제