public class SmsReciver extends BroadcastReceiver {
private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED"; public String newmessage;//static  @Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(SMS_RECEIVED)) {
Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdus.length; i++){
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
}
for(SmsMessage message : messages) {
newmessage = message.getMessageBody();
Intent newIntent = new Intent(context,AirFluxActivity.class);
// 在Service中启动Activity,必须设置如下标志
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(newIntent);        
}
}
}
}
}

解决方案 »

  1.   

    是有的时候Activity启动成功,有的时候不行么?
    不行的时候Log中有什么信息啊
      

  2.   

    06-09 09:45:09.502: ERROR/MediaPlayer(52): Unable to to create media player
    06-09 09:45:09.471: ERROR/MediaPlayerService(31): Couldn't open fd for content://settings/system/notification_sound
    06-09 09:45:09.502: ERROR/MediaPlayer(52): Unable to to create media player
    06-09 09:45:09.471: ERROR/MediaPlayerService(31): Couldn't open fd for content://settings/system/notification_sound
    06-09 09:45:09.502: ERROR/MediaPlayer(52): Unable to to create media player
    06-09 09:45:09.471: ERROR/MediaPlayerService(31): Couldn't open fd for content://settings/system/notification_sound
    06-09 09:45:09.502: ERROR/MediaPlayer(52): Unable to to create media player
    06-09 09:45:09.471: ERROR/MediaPlayerService(31): Couldn't open fd for content://settings/system/notification_sound
    06-09 09:45:09.502: ERROR/MediaPlayer(52): Unable to to create media player
    06-09 09:45:09.502: ERROR/MediaPlayer(52): Unable to to create media player
    06-09 09:45:09.502: ERROR/MediaPlayer(52): Unable to to create media player
    06-09 09:45:11.116: ERROR/MediaPlayerService(31): Couldn't open fd for content://settings/system/notification_sound
    06-09 09:45:11.116: ERROR/MediaPlayerService(31): Couldn't open fd for content://settings/system/notification_sound
    06-09 09:45:11.168: ERROR/MediaPlayer(52): Unable to to create media player
    06-09 09:45:11.168: ERROR/MediaPlayer(52): Unable to to create media player
    06-09 09:45:28.156: ERROR/ActivityThread(100): Failed to find provider info for android.server.checkin
    06-09 09:45:28.156: ERROR/ActivityThread(100): Failed to find provider info for android.server.checkin
    06-09 09:45:29.276: ERROR/MediaPlayerService(31): Couldn't open fd for content://settings/system/ringtone
    06-09 09:45:29.299: ERROR/MediaPlayer(100): Unable to to create media player
    06-09 09:45:29.299: ERROR/RingtoneManager(100): Failed to open ringtone content://settings/system/ringtone
    06-09 09:45:29.276: ERROR/MediaPlayerService(31): Couldn't open fd for content://settings/system/ringtone
    06-09 09:45:29.299: ERROR/MediaPlayer(100): Unable to to create media player
    06-09 09:45:29.299: ERROR/RingtoneManager(100): Failed to open ringtone content://settings/system/ringtone
      

  3.   

    06-09 09:47:01.207: WARN/ActivityManager(52): Activity pause timeout for HistoryRecord{44d9e9f8 com.android.phone/.InCallScreen}
    06-09 09:47:01.207: WARN/ActivityManager(52): Activity pause timeout for HistoryRecord{44d9e9f8 com.android.phone/.InCallScreen}
    06-09 09:47:02.226: WARN/InputManagerService(52): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@44e20d30
    06-09 09:47:02.226: WARN/InputManagerService(52): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@44e20d30
    06-09 09:47:02.816: WARN/ActivityManager(52): Activity pause timeout for HistoryRecord{44e79f58 com.android.phone/.InCallScreen}
    06-09 09:47:02.816: WARN/ActivityManager(52): Activity pause timeout for HistoryRecord{44e79f58 com.android.phone/.InCallScreen}
    06-09 09:47:02.816: WARN/ActivityManager(52): Activity pause timeout for HistoryRecord{44e79f58 com.android.phone/.InCallScreen}
    06-09 09:47:02.816: WARN/ActivityManager(52): Activity pause timeout for HistoryRecord{44e79f58 com.android.phone/.InCallScreen}
    06-09 09:47:02.816: WARN/ActivityManager(52): Activity pause timeout for HistoryRecord{44e79f58 com.android.phone/.InCallScreen}
    06-09 09:47:02.816: WARN/ActivityManager(52): Activity pause timeout for HistoryRecord{44e79f58 com.android.phone/.InCallScreen}
    06-09 09:47:02.816: WARN/ActivityManager(52): Activity pause timeout for HistoryRecord{44e79f58 com.android.phone/.InCallScreen}
    这些是警告日志
      

  4.   

    一般不建议使用广播启动Acitivity!
    可以在收到广播时,通过状态栏提示,启动Acitivityfor(SmsMessage message : messages) {
    newmessage = message.getMessageBody();
    Intent newIntent = new Intent(context,AirFluxActivity.class);
    PendintIntent pi=PendingIntent.getActivity(context,0,newIntent,0);
    Notification myNoti=new Notification();
    NotificationManager noti=(NotificationManager)context.getSystemService(Service.NOTIFICATION_SERVICE);
    myNoti.icon=R.drawable.icon;
    myNoti.tickerText="启动activity";
    myNoti.setLatestEventInfo(context,"service","启动activity",pi);
    noti.notify(0,myNoti);
    }楼主试试吧!
      

  5.   

    试过了,这个方法Acitivity根本就启动不了,而且程序还会报错
      

  6.   

    onReceive 里面做太多的事了,BroadcastReceiver大约10S就会被销毁,不管事情有没有完成。
      

  7.   

    请不要在广播里面处理界面元素的东西,因为一个广播的发出到结束可能会在10秒钟左右就会消失,但一个activity界面启动的时间可能会短也可能会长,所以就倒制您所说的有时候能启 有时候不能启。
      解决的办法就是在广播里面向handler发送一个消息,在handler里面去打开一个界面。
      
      

  8.   

    请看看sdk里面说的:
    A BroadcastReceiver object is only valid for the duration of the call to onReceive(Context, Intent). Once your code returns from this function, the system considers the object to be finished and no longer active. This has important repercussions to what you can do in an onReceive(Context, Intent) implementation: anything that requires asynchronous operation is not available, because you will need to return from the function to handle the asynchronous operation, but at that point the BroadcastReceiver is no longer active and thus the system is free to kill its process before the asynchronous operation completes. In particular, you may not show a dialog or bind to a service from within a BroadcastReceiver. For the former, you should instead use the NotificationManager API. For the latter, you can use Context.startService() to send a command to the service. (最后一句是介意您最好使用一个服务去启动一个界面,因为广播在处理onReceive函数后就会消毁)