在我的应用中,我使用Broadcast Receiver
来检测到达的短信息...
当我的应用程序运行时,运行良好...
但在被最近的应用程序杀死后,当有MessageBody:"silent"
的短信到达时,我的应用程序崩溃了......!
// silent
是我的SMS BroadcastReceiver中的一个动作......
这是我的代码:
清单:
<receiver android:name=".SmsReceiver" >
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
广播接收器:
public class SmsReceiver extends BroadcastReceiver {
public static String senderTel;
public static String messageBody;
@Override
public void onReceive(Context context, Intent intent) {
Object[] pdus = (Object[]) intent.getExtras().get("pdus");
SmsMessage sms = SmsMessage.createFromPdu((byte[]) pdus[0]);
senderTel = sms.getOriginatingAddress();
messageBody = sms.getMessageBody().toLowerCase().trim();
if(messageBody.equals("silent")){
G.AudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
G.AudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
Toast.makeText(G.currentActivity, "silent", Toast.LENGTH_SHORT).show();
}
}
G:
public class G extends Application {
public static Context context;
public static Activity currentActivity;
@Override
public void onCreate() {
super.onCreate();
context = getApplicationContext();
AudioManager= (AudioManager) getBaseContext().getSystemService(Context.AUDIO_SERVICE);
}
}
logcat的:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
所以我尝试使用服务和警报管理器但是他们失败了
Intent intent = new Intent(G.context, SmsReceiver.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getBroadcast(G.context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
G.alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 3000, pendingIntent);
即使应用程序被最近的应用程序杀死或强行关闭,我仍希望我的广播接收器保持活动状态......
是否有必要使用Service
......?
感谢您提供的任何帮助
答案 0 :(得分:3)
我认为这里的主要问题是,在您的func audioRouteChangeListenerCallback (notif: NSNotification){
let userInfo:[NSObject:AnyObject] = notif.userInfo!
println("\(userInfo)")
let routChangeReason = UInt((userInfo[AVAudioSessionRouteChangeReasonKey]?.integerValue)!)
switch routChangeReason {
case AVAudioSessionRouteChangeReason.NewDeviceAvailable.rawValue:
self.println("Headphone/Line plugged in");
break;
case AVAudioSessionRouteChangeReason.OldDeviceUnavailable.rawValue:
//If the headphones was pulled move to speaker
do {
try AVAudioSession.sharedInstance().overrideOutputAudioPort(AVAudioSessionPortOverride.Speaker)
} catch _ {
}
self.println("Headphone/Line was pulled. Stopping player....");
break;
case AVAudioSessionRouteChangeReason.CategoryChange.rawValue:
// called at start - also when other audio wants to play
self.println("AVAudioSessionRouteChangeReasonCategoryChange");
break;
default:
break;
}
}
中,您正在从您的应用程序(BroadcastReceiver
)调用引用,并且由于应用程序已经死亡,因此G.currentActivity
崩溃。问题不在于您的BroadcastReceiver无法正常工作......
答案 1 :(得分:3)
尝试更改:
Toast.makeText(G.currentActivity, "silent", Toast.LENGTH_SHORT).show();
到
Toast.makeText(context, "silent", Toast.LENGTH_SHORT).show();
应用程序正在被杀死,而不是BroadcastReceiver。在android需要时调用接收器。这里的解决方案是使用方法参数提供的上下文而不是可能为null的活动。
如果您只想在打开应用程序时显示toast,请考虑添加一项检查以查看currentActivity是否为null(假设它已在onStart上分配并在onStop上清除),或者在活动中注册接收者。