本文介绍了使用2嵌套类来获得新的短信通知:D的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经问了一个问题在这里:
combining-2-extended-activity-for-sms-notification

I already asked a question here :combining-2-extended-activity-for-sms-notification

,现在我得到一个新的问题:D

And now i get a new problem :D

所以,我已经做出嵌套类是这样的:

So i already make a nested Class like this :

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>

问题是,我的应用程序现在无法检测到任何新的SMS ...
哪里是我的错?是它在我的表现?
我已经尝试过改变我的codeS,但我仍然解决不了我的问题...
所以,问题是:
您能帮我,让我的应用程序可以检测到新的短信和发出通知每次新的SMS自带?
非常感谢你! :D
索里和如果我犯了一些错误,英语不是我的母语的语:)
我有一个想法:我试图扭转类,所以SMSReceiver将是外层和SMSNotif将里面它...它是可能的(我尝试了,我得到了一些错误),我认为反?它的广播接收器将能够检测到新SMS..is是真的吗?

推荐答案

您应该指定一个接收器是在明显的内部类。

You should specify that your receiver is in inner class in manifest.

     <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>

该接收器不应该在活动。

The Receiver should not be in activity.

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

and your inner class SMSReceiver should be static

EDITED
如果你想只显示通知,然后活动并不是必需的。

EDITEDIf you want to just show the notification then Activity is not Required.

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);
        }
    }
}

这篇关于使用2嵌套类来获得新的短信通知:D的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 19:12