0

SMS経由で受信したメッセージテキストをメインアクティビティのテキストビューに設定したいと考えています。私は懸命に努力し、インターネットのあらゆる場所を検索しましたが、うまくいきません。私を助けてください

他のアクティビティの TextView に Text を設定するにはどうすればよいですか? 私はそれをやろうとしましたが、うまくいきません

メソッドは「TextView textViewAlarmMassage = (TextView) findViewById(R.id.textViewAlarmMassage);」だと思います。プログラムkmがこの行で停止しているため、mainActivityは機能しません

主な活動

public class MainActivity extends Activity {

//ここに別のコードがあります

public void showAlarmMassage(){

    //Connect to SMSReceiver Class
    *SMSReceiver receiver = new SMSReceiver();*

    //Get the Alarm Massage
    *String massage = receiver.getAlarmMassage().toString();*

    //Initialize the TextView
    *TextView textViewAlarmMassage = (TextView) findViewById(R.id.textViewAlarmMassage);*

    //Set the Alarm Massage in the TextView
    *textViewAlarmMassage.setText(massage);*
}   

}


public class SMSReceiver は BroadcastReceiver を拡張します{

public String alarmMassage; 
public String senderAddress;


@Override
public void onReceive(Context cxt, Intent intent){

    if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED"))
    {
        Bundle bundle = intent.getExtras();
        Object messages[] = (Object[]) bundle.get("pdus");
        SmsMessage smsMessage[] = new SmsMessage[messages.length];

        for (int n = 0; n < messages.length; n++)
            smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);

        alarmMassage = smsMessage[0].getDisplayMessageBody();
        senderAddress = smsMessage[0].getDisplayOriginatingAddress();

        //Set the AlarmMassage in the Setter Constructor
        *setAlarmMassage(alarmMassage);*            

        *MainActivity mainActivity = new MainActivity();*

        //Here I want to start the showAlarmMassage methode in the MainActivity
        *mainActivity.showAlarmMassage();*

        String toast = "Received SMS from: " + senderAddress;
        toast += "\nMessage: " + alarmMassage;

        Toast.makeText(cxt, toast, Toast.LENGTH_LONG).show();


    }
}

//Getting AlarmMassage
*public String getAlarmMassage(){*
    return this.alarmMassage;       
}

//setting AlarmMassage
*public void setAlarmMassage(String alarmMassage){*
    this.alarmMassage = alarmMassage;
}

}


マニフェスト

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="at.ffnt.datenbank"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" > </uses-permission>
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_startseite" >

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />


    </intent-filter>


    </activity>
    <activity
        android:name=".EntryActivity"
        android:label="@string/title_activity_entry" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".ViewAllEntriesActivity"
        android:label="@string/title_activity_view_all_entries" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name=".AudioRecording"
        android:label="@string/title_audio_recording" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver android:name=".StatusReceiver">...</receiver>
<receiver android:name=".SMSReceiver">
    <intent-filter>
        <action android:name="android.provider.Telephony.SMS_RECEIVED" />
    </intent-filter>
</receiver>


</application>


ログ

01-03 17:04:30.598: E/AndroidRuntime(22422): 致命的な例外: メイン 01-03 17:04:30.598: E/AndroidRuntime(22422): java.lang.RuntimeException: Unable to start receiver at.ffnt. datenbank.SMSReceiver: java.lang.NullPointerException 01-03 17:04:30.598: E/AndroidRuntime(22422): android.app.ActivityThread.handleReceiver(ActivityThread.java:2287) 01-03 17:04:30.598: E /AndroidRuntime(22422): android.app.ActivityThread.access$1600(ActivityThread.java:140) 01-03 17:04:30.598: E/AndroidRuntime(22422): android.app.ActivityThread$H.handleMessage(ActivityThread) .java:1313) 01-03 17:04:30.598: E/AndroidRuntime(22422): android.os.Handler.dispatchMessage(Handler.java:99) 01-03 17:04:30.598: E/AndroidRuntime(22422) ): android.os.Looper.loop(Looper.java:137) 01-03 17:04:30.598: E/AndroidRuntime(22422): android.app.ActivityThread で。main(ActivityThread.java:4921) 01-03 17:04:30.598: E/AndroidRuntime(22422): java.lang.reflect.Method.invokeNative(ネイティブ メソッド) 01-03 17:04:30.598: E/AndroidRuntime (22422): java.lang.reflect.Method.invoke(Method.java:511) 01-03 17:04:30.598: E/AndroidRuntime(22422): com.android.internal.os.ZygoteInit$MethodAndArgsCaller で。実行 (ZygoteInit.java:1038) 01-03 17:04:30.598: E/AndroidRuntime(22422): com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805) 01-03 17:04: 30.598: E/AndroidRuntime(22422): dalvik.system.NativeStart.main(Native Method) 01-03 17:04:30.598: E/AndroidRuntime(22422): 原因: java.lang.NullPointerException 01-03 17: 04:30.598: E/AndroidRuntime(22422): at.ffnt.datenbank.MainActivity.showAlarmMassage(MainActivity.java:248) 01-03 17:04:30.598: E/AndroidRuntime(22422): at.ffnt.datenbank .SMSReceiver.onReceive(SMSReceiver.java:37) 01-03 17:04:30.598: E/AndroidRuntime(22422): android.app.ActivityThread.handleReceiver(ActivityThread.java:2280) 01-03 17:04:30.598: E/AndroidRuntime(22422): ... 10 以上

4

1 に答える 1

0

放送受信機

public class SMSReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context cxt, Intent intent)
 {
  if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED"))
   {
     Bundle b=intent.getExtras();
     Intent i=new Intent(getApplicationContext(),GetSMSActivity.class);
     i.putExtra("SMS_BUNDLE",b);
     startActivity(i);
     
   }
  }
 }

マニフェスト ファイル

 <uses-permission android:name="android.permission.RECEIVE_SMS" />
 <activity
    android:name=".GetSMSActivity"
    android:label="@string/title_activity_startseite" >
  <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>
</activity>
<receiver android:name=".SMSReceiver">
<intent-filter>
    <action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>

あなたのメインアクティビティ

GetSMSActivity.java


TextView textViewAlarmMassage = (TextView) findViewById(R.id.textViewAlarmMassage);
Intent smsReceived=getIntent();
Bundle smsData=smsReceived.getExtras("SMS_BUNDLE");
Object messages[] = (Object[])smsData.get("pdus");
SmsMessage smsMessage[]=new SmsMessage[messages.length];
    
for (int n = 0; n < messages.length; n++)
     {
        smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
      }
String alarmMassage = smsMessage[0].getDisplayMessageBody();
String senderAddress = smsMessage[0].getDisplayOriginatingAddress();

textViewAlarmMassage.setText(alarmMassage);

テスト時にEclipseを使用してメッセージをシミュレートします。

于 2013-01-03T17:49:56.307 に答える