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);
}
}
}
}
}
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);
}
}
}
}
}
解决方案 »
- Bitmap类型的图片怎样放到ImageSwitcher中
- 在android中,删除SD卡里的数据后刷新数据的问题
- Ophone 适配屏幕大小问题
- 我想让下面的一排button始终处于屏幕最下端
- 高分求助:Android多语言【时间】的问题
- 备份与恢复问题
- 恶劣网络环境,android客户端和服务器数据同步方案
- android4.4.4源码编译报错
- android 可以时时监听网络状况,那如何用BroadcastReceiver监听 一个变量是否发生变化
- android activity 生命周期怎么和别人的有所不同,设置启动模式也没什么用
- Log.d("",""+i)这样调试程序需要考虑多线程吗?
- ExpandableListView中组item的默认icon如何让更改?
不行的时候Log中有什么信息啊
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
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}
这些是警告日志
可以在收到广播时,通过状态栏提示,启动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);
}楼主试试吧!
解决的办法就是在广播里面向handler发送一个消息,在handler里面去打开一个界面。
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函数后就会消毁)