这是我的第一个安卓程序。我有一个可以大声读取信息的活动,一个侦听传入 SMS 消息的接收器和一个服务,以使应用程序在后台运行。
我的主要活动有一个切换按钮。开启时,它会启动创建通知以防止应用程序被杀死的服务。用户也可以点击通知返回主活动:
package com.rockmanx77777.SMSbyVoice;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Binder;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
public class SMSService extends Service{
@Override
public void onCreate() {
createNotification();
super.onCreate();
}
public class MyBinder extends Binder {
SMSService getService() {
return SMSService.this;
}
}
private final IBinder binder = new MyBinder();
@Override
public IBinder onBind(Intent intent) {
return binder;
}
private void createNotification(){
final int NOTIFICATION_ID = 77777;
Intent intent = new Intent(this, SMSByVoice.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("senderNumber", "");
intent.setAction("com.rockmanx77777.SMSByVoice.PROCESS");
PendingIntent pi = PendingIntent.getActivity(this, 1, intent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentTitle("Sample Title");
builder.setContentText("Sample Text");
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
builder.setContentIntent(pi);
Notification notification = builder.getNotification();
notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;
startForeground(NOTIFICATION_ID, notification);
}
}
当有传入的 SMS 时,服务的 onReceive 被正确调用。一切都得到执行,包括context.startActivity(localSMS);
我在这里遇到问题:
package com.rockmanx77777.SMSbyVoice;
import java.util.Set;
import android.annotation.TargetApi;
import android.app.ActivityManager;
import android.app.IntentService;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.telephony.SmsMessage;
import android.util.Log;
public class SMSReceiver extends BroadcastReceiver{
public SMSReceiver(){
}
@Override
public void onReceive(Context context, Intent intent) {
Log.d("MSG", "Receiver's onReceived Called");
if(intent.getAction() == "android.provider.Telephony.SMS_RECEIVED"){
//Get the SMS message passed in
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String senderNumber = "";
String body = "";
if (bundle != null){
//Retrieve the SMS message received
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]);
body = msgs[i].getMessageBody().toString();
}
senderNumber = msgs[0].getOriginatingAddress();
Intent localSMS = new Intent(context, SMSByVoice.class);
localSMS.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
localSMS.setAction("rockmanx77777.SMSByVoice.RETURN");
localSMS.putExtra("senderNumber", senderNumber);
localSMS.putExtra("body", body);
Log.d("MSG", "Starting activity from Receiver");
context.startActivity(localSMS);
}
}
else{
Log.d("MSG", "intent.getAction() != android.provider.Telephony.SMS_RECEIVED");
}
}
}
如果活动在前台运行,则一切正常。如果活动在后台运行(已创建服务和通知),context.startActivity(localSMS);
则将在 SMS 到达时执行,但onNewIntent()
不会调用活动的方法,也不会将活动带到前台。
如果我在执行后的某个时间手动单击通知,context.startActivity(localSMS);
那么活动将进入前台(如预期的那样)并现在执行onNewIntent()
。
我想要发生的是,如果活动在后台运行(创建服务和通知),那么在 SMS 到达时,我的接收器会将活动带到前台(通过startActivity(localSMS);
),然后,活动将调用它onNewIntent()
方法。
我不知道怎么了。我认为服务和接收器相互冲突,但我可能会完全关闭。抱歉这么久。我通常可以在这里找到我的问题的答案,但是对于这个问题我没有运气。任何帮助是极大的赞赏!