Android 使用2个嵌套类从新SMS获取通知:D

Android 使用2个嵌套类从新SMS获取通知:D,android,notifications,sms,Android,Notifications,Sms,我已经在这里问了一个问题: 现在我遇到了一个新问题:D 我已经创建了一个嵌套类,如下所示: public class SMSNotif extends Activity{ static final int HELLO_ID = 1; BroadcastReceiver myReceiver = null; public class SMSReceiver extends BroadcastReceiver { @Override public void onReceive(

我已经在这里问了一个问题:

现在我遇到了一个新问题:D

我已经创建了一个嵌套类,如下所示:

public class SMSNotif extends Activity{
static final int HELLO_ID = 1;
BroadcastReceiver myReceiver = null;

public class SMSReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context arg0, Intent arg1) {
        // TODO Auto-generated method stub

        Bundle bundle = arg1.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(arg0, str, Toast.LENGTH_SHORT).show();
        }
        //Intent i = new Intent(SMSReceiver.this, SMSNotif.class);
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    //String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    //int icon = R.drawable.ic_launcher;
    String tickerText = "Hello";
    //long when = System.currentTimeMillis();

    Notification notification = new Notification(R.drawable.ic_launcher, tickerText, System.currentTimeMillis());

    //Context context = getApplicationContext();
    String contentTitle = "My notification";
    String contentText = "Hello World!";
    Intent notificationIntent = new Intent(this, SMSNotif.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

    notification.setLatestEventInfo(this, contentTitle, contentText, contentIntent);
    notification.defaults = Notification.DEFAULT_ALL;
    mNotificationManager.notify(HELLO_ID, notification);

}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    if(myReceiver != null){
        unregisterReceiver(myReceiver);
        myReceiver = null;
    }
    super.onPause();
}

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

    if(myReceiver == null){
        myReceiver = new SMSReceiver();
        IntentFilter filter = new IntentFilter();
        registerReceiver(myReceiver, filter);
    }
}
 <activity
        android:name=".SMSNotif"
        android:label="@string/app_name" >
         <receiver android:name=".SMSReceiver" >
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>
    </activity>
公共类SMSNotif扩展活动{
静态最终整数HELLO_ID=1;
BroadcastReceiver myReceiver=null;
公共类SMSReceiver扩展了BroadcastReceiver{
@凌驾
公共void onReceive(上下文arg0,意图arg1){
//TODO自动生成的方法存根
Bundle Bundle=arg1.getExtras();
SmsMessage[]msgs=null;
字符串str=“”;
if(bundle!=null){
Object[]pdus=(Object[])bundle.get(“pdus”);
msgs=新SMS消息[PDU.length];
对于(int i=0;i
}

我的清单是这样的:

public class SMSNotif extends Activity{
static final int HELLO_ID = 1;
BroadcastReceiver myReceiver = null;

public class SMSReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context arg0, Intent arg1) {
        // TODO Auto-generated method stub

        Bundle bundle = arg1.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(arg0, str, Toast.LENGTH_SHORT).show();
        }
        //Intent i = new Intent(SMSReceiver.this, SMSNotif.class);
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    //String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    //int icon = R.drawable.ic_launcher;
    String tickerText = "Hello";
    //long when = System.currentTimeMillis();

    Notification notification = new Notification(R.drawable.ic_launcher, tickerText, System.currentTimeMillis());

    //Context context = getApplicationContext();
    String contentTitle = "My notification";
    String contentText = "Hello World!";
    Intent notificationIntent = new Intent(this, SMSNotif.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

    notification.setLatestEventInfo(this, contentTitle, contentText, contentIntent);
    notification.defaults = Notification.DEFAULT_ALL;
    mNotificationManager.notify(HELLO_ID, notification);

}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    if(myReceiver != null){
        unregisterReceiver(myReceiver);
        myReceiver = null;
    }
    super.onPause();
}

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

    if(myReceiver == null){
        myReceiver = new SMSReceiver();
        IntentFilter filter = new IntentFilter();
        registerReceiver(myReceiver, filter);
    }
}
 <activity
        android:name=".SMSNotif"
        android:label="@string/app_name" >
         <receiver android:name=".SMSReceiver" >
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>
    </activity>

问题是,我的应用程序现在无法检测到任何新的短信。。。 我的错在哪里?在我的舱单上吗? 我已经试着改变我的代码,但我仍然不能解决我的问题。。。 因此,问题是: 你能帮助我让我的应用程序检测新短信并在每次新短信出现时发出通知吗? 非常感谢D 很抱歉,如果我犯了一些错误,英语不是我的母语
我有一个想法:我尝试反转该类,因此SMSReceiver将是外部的,而SMSNotif将是内部的…可能吗?(我尝试过,但出现了一些错误)我认为通过反转它,广播接收器将能够检测到新的短信。这是真的吗?我不确定它是否能解决您的问题,但我认为,您应该尝试对清单文件进行以下更改:

<activity
        android:name=".SMSNotif"
        android:label="@string/app_name" >        
</activity>
<receiver android:name="com.your.path.SMSNotif$SMSReceiver" >
        <intent-filter android:priority="100" >
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
</receiver>

我希望它能解决您的问题。因为广播接收器是SMSNotif类的内部类,所以您必须提供完整路径以及ActivityName$ReceiverName,如上所示

编辑:


我已经添加了意向过滤器标签(见上述代码)。这将在通知android操作系统新短信之前通知你的应用程序。

你应该在清单中指定你的接收者在内部类中

     <activity
            android:name=".SMSNotif"
            android:label="@string/app_name" >

        </activity>

 <receiver android:name="your.package.name.SMSNotif$SMSReceiver" >
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>

接收者不应处于活动状态

您的内部类SMSReceiver应该是静态的

编辑 如果只想显示通知,则不需要活动

public class SMSReceiver extends BroadcastReceiver {

    private static final int HELLO_ID = 0;

    @Override
    public void onReceive(Context arg0, Intent arg1) {
        // TODO Auto-generated method stub

        Bundle bundle = arg1.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(arg0, str, Toast.LENGTH_SHORT).show();

            // String ns = Context.NOTIFICATION_SERVICE;
            NotificationManager mNotificationManager = (NotificationManager) arg0
                    .getSystemService(Context.NOTIFICATION_SERVICE);

            // int icon = R.drawable.ic_launcher;
            String tickerText = "Hello";
            // long when = System.currentTimeMillis();

            Notification notification = new Notification(
                    R.drawable.ic_launcher, tickerText,
                    System.currentTimeMillis());

            // Context context = getApplicationContext();
            String contentTitle = "My notification";
            String contentText = "Hello World!";
            Intent notificationIntent = new Intent(arg0, SMSNotif.class);
            PendingIntent contentIntent = PendingIntent.getActivity(arg0, 0,
                    notificationIntent, 0);

            notification.setLatestEventInfo(arg0, contentTitle, contentText,
                    contentIntent);
            notification.defaults = Notification.DEFAULT_ALL;
            mNotificationManager.notify(HELLO_ID, notification);
        }
    }
}
公共类SMSReceiver扩展了BroadcastReceiver{
私有静态final int HELLO_ID=0;
@凌驾
公共void onReceive(上下文arg0,意图arg1){
//TODO自动生成的方法存根
Bundle Bundle=arg1.getExtras();
SmsMessage[]msgs=null;
字符串str=“”;
if(bundle!=null){
Object[]pdus=(Object[])bundle.get(“pdus”);
msgs=新SMS消息[PDU.length];
对于(int i=0;i