0

SMSメッセージをキャプチャするようにブロードキャストレシーバーを設定しました。クラスはトーストを使用してメッセージを表示しますが、文字列をファイルに書き込む必要がありますが、アクティビティではないため機能しません。

ブロードキャストレシーバー内のファイルに「smsメッセージ」文字列を書き込む方法はありますか?

そうでない場合は、テキストメッセージを受信したときにアプリを実行せずにSMSメッセージをファイルに保存する別の方法を誰かが考えることができますか?

   public class SMSReciever extends BroadcastReceiver {

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

   //---get the SMS message passed in---

   Bundle bundle = intent.getExtras();        
   SmsMessage[] msgs = null;
   String str = "";            
   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]);                
           str += SMS from " + msgs[i].getOriginatingAddress();                     
           str += " :";
           str += msgs[i].getMessageBody().toString();
           str += "\n";        
       }


       //---display the new SMS message---

       Toast.makeText(context, str, Toast.LENGTH_SHORT).show();

        //---Save SMS message---

          SaveMessage(str);

              }
        }


       public void SaveMessage(String message) {

    FileOutputStream FoutS = null;

    OutputStreamWriter outSW = null;

    try {

        FoutS = openFileOutput("Message.dat", MODE_PRIVATE);

        outSW = new OutputStreamWriter(FoutS);

        outSW.write(message);

        outSW.flush();

    }

    catch (Exception e) {

        e.printStackTrace();

    }

    finally {

        try {

            outSW.close();

            FoutS.close();

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

}

構文エラー「MODE_PRIVATEを変数に解決できません」
-コードはアクティビティクラスで使用された場合にのみ機能します

マニフェストファイル:

        <receiver android:name=".SMSReciever"> 
        <intent-filter> 
            <action android:name=
                "android.provider.Telephony.SMS_RECEIVED"
                android:enabled="true" /> 

        </intent-filter> 
    </receiver>

これは私の初めての投稿なので、すべてが正しく行われたことを願っています。このサイトはすでに私を大いに助けてくれました。

4

3 に答える 3

3

...に渡しcontextてみてくださいSaveMessage

SaveMessage(context, str);

...そして、次のようにSaveMessageメソッドを変更します...

public void SaveMessage(Context context, String message) {

    FileOutputStream FoutS = null;
    OutputStreamWriter outSW = null;

    try {
        FoutS = context.openFileOutput("Message.dat", Context.MODE_PRIVATE);

        // Rest of try block here
    }
    // 'catch' and 'finally' here
}
于 2012-01-15T01:23:59.943 に答える
0

以下を使用して、データのアプリケーション プライベート ファイル オブジェクトを取得できます。

File outFile = new File(getExternalFilesDir(null), "DemoFile.jpg");

( にContext渡されたオブジェクトを使用しますonReceive。)次に、次の標準クラスを使用して、オブジェクトを開いて書き込むことができますjava.io

Writer writer = new FileWriter(outFile);
. . .

外部ファイル ディレクトリ内のファイルは、アプリをアンインストールすると削除されます。

于 2012-01-15T01:09:02.643 に答える
0

SDCard への書き込み権限がありません。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  

良いコード サンプルはこちらです
http://androidgps.blogspot.com/2008/09/writing-to-sd-card-in-android.html

ただし、SD カードのルートには書き込みません。/data/[アプリケーション パッケージ]/ に書き込むことをお勧めします。

[アプリケーション パッケージ] をパッケージに置き換えます。例: com.example.helloandroid

于 2012-01-15T01:55:18.060 に答える