2011-07-31 6 views
0

ここは新しいです。私はSMSメッセージに警告を表示する必要があるアプリを作っている。 私はどのように私が理解しているとは思わない。関数が "this"属性を持っている場合、別のクラスから関数を呼び出すことができます。smsRecevicerクラスのアラートに関する問題

しかし、この行は動作しません:

AlertDialog.Builder(this).setTitle("asd").setMessa ge(str).setNegativeButton("Annuller", null).setPositiveButton("Bekræft", null).show(); 

を、私はそれがあるため、このクラスの実行Iの背景または何であるかどうかわからないのですか?

これはクラスです:

package net.sms; 

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

public class SmsReceiver 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";   
      } 
      AlertDialog.Builder alt_bld = new AlertDialog.Builder(context); 
      alt_bld.setTitle("aaa"); 
      alt_bld.show(); 
      //new AlertDialog.Builder(this).setTitle("asd").setMessage(str).setNegativeButton("Annuller", null).setPositiveButton("Bekræft", null).show(); 
      //---display the new SMS message--- 
      Toast.makeText(context, str, Toast.LENGTH_SHORT).show(); 
     }       
    } 
} 

答えて

0

ブロードキャスト受信機でアラートを開始することはできません。アラートのようなアクティビティを開始することで、同様の効果を得ることができます。 注:バックグラウンドサービスや受信者からの活動を開始することはお勧めしません。

たManifest.xml

<activity android:name="AlertActivity" android:theme="@android:style/Theme.Dialog" /> 

alert_activity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="fill_parent"> 
<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content"> 

    <Button android:id="@+id/positiveButton" android:layout_weight="1" 
     android:layout_width="wrap_content" android:layout_height="wrap_content" 
     android:text="Annuller" /> 

    <Button android:id="@+id/negativeButton" android:layout_weight="1" 
     android:layout_width="wrap_content" android:layout_height="wrap_content" 
     android:text="Bekraft" /> 

</LinearLayout> 
</LinearLayout> 

SmsReceiver.java

context.startActivity(new Intent(context, AlertActivity.class)); 
0

レシーバは、アラートダイアログを好きではありません。あなたは通知やトーストを使うことができます。

関連する問題