这是我的第一个Android程序。我有一个活动可以读取信息,一个收听短信和服务的接收器,以便让应用程序在后台运行。
我的主要活动有一个切换按钮。启用时,它会启动服务,该服务会创建通知以防止应用程序被杀死。用户还可以单击通知以返回主活动:
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);
}
}
当有传入的短信时,正确调用服务的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()
。
我想要发生的是,如果活动在后台运行(服务和通知创建),那么在短信到达时,我的接收器会将活动带到前台(通过startActivity(localSMS);
),以及在此之后,活动将调用其onNewIntent()
方法。
我不知道出了什么问题。我认为服务和接收器是相互冲突的,但我可以完全关闭。对不起,这么久了。我通常可以在这里找到问题的答案,但对于这个问题,我没有运气。任何帮助是极大的赞赏!
答案 0 :(得分:0)
我弄明白了什么。我的服务中没有接收器:我的服务类已更改为:
public class SMSService extends Service{
SMSReceiver receiver = new SMSReceiver();
@Override
public void onCreate() {
Log.d("MSG", "SMSService OnCreate Started");
createNotification();
this.registerReceiver(receiver, new IntentFilter("android.provider.Telephony.SMS_RECEIVED"));
super.onCreate();
Log.d("MSG", "SMSService OnCreate Ended");
}
public class MyBinder extends Binder {...