0

私のコードには、これを含むJavaファイルがあります:

package com.myapp.basic;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;

public class SMSReceiverActivity extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // Parse the SMS.
        Bundle bundle = intent.getExtras();
        SmsMessage[] msgs = null;
        String str = "";
        if (bundle != null)
        {
            // Retrieve the SMS.
            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]);
                // In case of a particular App / Service.
                //if(msgs[i].getOriginatingAddress().equals("+91XXX"))
                //{
                //str += "SMS from " + msgs[i].getOriginatingAddress();
                //str += " :";
                str += msgs[i].getMessageBody().toString();
                str += "\n";
                //}
            }
            if (str != "") { // remove the last \n
                str = str.substring(0, str.length()-1);
            }

            // Need to find a better way to check if the activity is running
            try {
                ((Police_ViewActivity) context.getApplicationContext()).handle_incoming_help_message(str); // crash here
            } catch(Exception e) {
                System.out.println(e.getMessage());
            }
            System.out.println("ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ");
        }
    }
}

try ブロックのコードが発生するとクラッシュし、次のエラーが発生します: 08-27 22:45:11.345: I/System.out(27606): android.app.Application cannot be cast to com.escortme.basic. Police_ViewActivity

どうすればこれを修正できますか?

4

2 に答える 2

0

別のクラスで handle_incoming_help_message メソッドを呼び出す必要がある場合は、handle_incoming_help_message メソッドを静的にします。

update:

したがって、Police_ViewActivity を開始して、handle_incoming_help_message を呼び出す必要があることを伝える必要があります。

Intent intent = new Intent(context, Police_ViewActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
intent.putExtra("handle_incoming_help_message", str);
context.startActivity(intent);

および Police_ViewActivity の onCreate (または onResume または Onstart) で

String str = getIntent().getStringExtra("handle_incoming_help_message");
if(str != null)
    handle_incoming_help_message(str);

Update 2

policeviewactivity のインスタンス数を最大 1 に保つには? -> Android LaunchMode

于 2012-08-28T03:07:40.230 に答える
0

アクティビティの呼び出しには、 を使用するのが最適Intentです。次のようなものを置くことができます:

Intent intent = new Intent(Police_ViewActivity.class);
intent.putExtra("help_message", str);

context.startActivity(intent);

Police ビュー アクティビティの onResume() で、エクストラを取得して処理します。

onResume(){
    String msg = getIntent().getStringExtra("help_message");
    if (msg==null) return;
    handleMessage(msg);
}

アクティビティの複数のインスタンスが作成されることを心配する必要はありません。マニフェストでは、Launchmodeを使用してアクティビティの起動方法を指定できます。singleInstance を選択できます。

于 2012-08-28T03:01:04.537 に答える