且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

无法实例活动ComponentInfo Android中接收短信应用

更新时间:2022-11-22 16:51:03

您广播接收器即ReceivesmsActivity和活动即ReceivesmsActivity是具有相同的名称可能会引起问题,所以只是改变以不同的名称注册您的短信接收器:

 <接收机器人:名字=。ReceivesmsBroadcast>
        &所述;意图滤光器>
            <作用机器人:名字=android.provider.Telephony.SMS_RECEIVED/>
        &所述; /意图滤光器>
    < /接收器>

在code创建一个类ReceivesmsBroadcast.java:

 公共类ReceivesmsBroadcast扩展广播接收器{ 私有静态最后弦乐TAG =消息收到; @覆盖
 公共无效的onReceive(上下文的背景下,意图意图){
///您的code去这里 }
 }

Facing the Unable to instantiate activity ComponentInfo problem while launching the SMS Receiver Application. Could any tell me what i am doing wrong????

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.raja.receivesms"
  android:versionCode="1"
  android:versionName="1.0.0">
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name">
    <activity android:name=".ReceivesmsActivity"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver android:name=".ReceivesmsActivity"> 
        <intent-filter> 
            <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
        </intent-filter> 
    </receiver>
</application>
<uses-permission android:name="android.permission.RECEIVE_SMS">
</uses-permission>
<uses-sdk android:minSdkVersion="8" /></manifest>

And class file as follows ReceivesmsActivity.java

    package com.raja.receivesms;
import android.content.*;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;
import android.content.Intent;

public class ReceivesmsActivity extends BroadcastReceiver {

 private static final String TAG = "Message recieved";

 @Override
 public void onReceive(Context context, Intent intent) {    
     Bundle bundle = intent.getExtras();        
     SmsMessage[] msgs = null;
     String str = "";            
     if (bundle != null)
     {
         Object[] pdus = (Object[]) bundle.get("pdus");
         msgs = new SmsMessage[pdus.length];            
         for (int i=0; i<msgs.length; i++){
             msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                
             str += "SMS from " + msgs[i].getOriginatingAddress();                     
             str += " :";
             str += msgs[i].getMessageBody().toString();
             str += "\n";        
         }
         Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
     }                         
 }
 }

Your BroadcastReceiver i.e ReceivesmsActivity and Activity i.e ReceivesmsActivity is with same name may be causing problem so just change to different name register your sms receiver as:

<receiver android:name=".ReceivesmsBroadcast"> 
        <intent-filter> 
            <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
        </intent-filter> 
    </receiver>

and in code create a class ReceivesmsBroadcast.java:

public class ReceivesmsBroadcast extends BroadcastReceiver {

 private static final String TAG = "Message recieved";

 @Override
 public void onReceive(Context context, Intent intent) {    
///     your code go here      

 }
 }