2

これは私の最初の Android プログラムです。情報を読み上げる 1 つのアクティビティ、着信 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()方法。

何が悪いのかわからない。サービスと受信者が競合していると思いますが、完全にずれている可能性があります。長くなってすみません。通常、ここで問題の解決策を見つけることができますが、この問題については運がありませんでした。どんな助けでも大歓迎です!

4

1 に答える 1

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 {...
于 2012-08-15T01:35:37.410 に答える