Android:活动未从后台执行onNewIntent()(从服务调用)
这是我的第一个Android程序。我有一个活动可以大声读取信息,一个接收器可以监听收到的短信,还有一个服务可以让应用程序在后台工作 我的主要活动有一个切换按钮。启用时,它将启动服务,该服务将创建一个通知,以防止应用程序被终止。用户还可以单击通知返回到主活动:Android:活动未从后台执行onNewIntent()(从服务调用),android,service,android-activity,notifications,foreground,Android,Service,Android Activity,Notifications,Foreground,这是我的第一个Android程序。我有一个活动可以大声读取信息,一个接收器可以监听收到的短信,还有一个服务可以让应用程序在后台工作 我的主要活动有一个切换按钮。启用时,它将启动服务,该服务将创建一个通知,以防止应用程序被终止。用户还可以单击通知返回到主活动: package com.rockmanx77777.SMSbyVoice; import android.app.Notification; import android.app.PendingIntent; import 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);
}
}
当有传入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");
}
}
}
package com.rockmanx7777.SMSbyVoice;
导入java.util.Set;
导入android.annotation.TargetApi;
导入android.app.ActivityManager;
导入android.app.IntentService;
导入android.content.BroadcastReceiver;
导入android.content.Context;
导入android.content.Intent;
导入android.content.pm.PackageManager;
导入android.os.Bundle;
导入android.support.v4.content.LocalBroadcastManager;
导入android.telephony.sms消息;
导入android.util.Log;
公共类SMSReceiver扩展了BroadcastReceiver{
公共SMSReceiver(){
}
@凌驾
公共void onReceive(上下文、意图){
Log.d(“MSG”,“收到呼叫的接收器”);
if(intent.getAction()=“android.provider.Telephony.SMS_RECEIVED”){
//获取传入的SMS消息
Bundle=intent.getExtras();
SmsMessage[]msgs=null;
字符串senderNumber=“”;
字符串体=”;
if(bundle!=null){
//检索收到的SMS消息
Object[]pdus=(Object[])bundle.get(“pdus”);
msgs=新SMS消息[PDU.length];
对于(int i=0;i我找出了缺少的内容。我的服务中没有接收器:我的服务类更改为:
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 {...
我找到了丢失的内容。我的服务中没有接收器:我的服务类更改为:
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 {...