0

我在收到他的短信时使用 alertbox 短信创建应用程序。但是我遇到了麻烦,因为收到短信时没有出现警报框代码。

我的代码:

//SMSReceiver

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class SMSReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {       
        Intent startIntent = new Intent(context, SMSNotif.class);
        startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(startIntent);
    }
}
//SMSNotif


import android.app.Activity;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
public class SMSNotif extends Activity 
{
private static final String LOG_TAG = "SMSReceiver";
public static final int NOTIFICATION_ID_RECEIVED = 0x1221;
static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    IntentFilter filter = new IntentFilter(ACTION);
    this.registerReceiver(registerReceiver, filter);
}
private void displayAlert()
{
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Are you sure you want to exit?").setCancelable(
            false).setPositiveButton("Yes",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            }).setNegativeButton("No",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });
    AlertDialog alert = builder.create();
    alert.show();
}
private final BroadcastReceiver registerReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (ACTION.equals(action)) 
        {
            //your SMS processing code
            displayAlert();
        }
    }
};
}

如果程序没有用完alertbox,是怎么回事?

4

1 回答 1

0

为了在收到短信时显示 AlertDialog,请将您的代码更改为:

步骤1:

为传入的 SMS 通知注册一个 Reciver,如下所示AndroidManifest.xml

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

和 SMS_RECEIVED 权限AndroidManifest.xml

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


第 2 步: 更改您的SMSReceiver.java身份:

public class SMSReceiver extends BroadcastReceiver
{
    static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";
    @Override
    public void onReceive(Context context, Intent intent)
    {   
               String action = intent.getAction();
        if (ACTION.equals(action)) 
        {
            //Start Activity here
             Intent startIntent = new Intent(context, SMSNotif.class);
                     startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                     context.startActivity(startIntent);
        }    
    }
} 


第 3 步:SMSNotif.java将您的活动代码 更改为:

public class SMSNotif extends Activity 
{
private static final String LOG_TAG = "SMSReceiver";
public static final int NOTIFICATION_ID_RECEIVED = 0x1221;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
     setContentView(R.layout.Activity_layout);
     // call displayAlert() here
       displayAlert();
}
    private void displayAlert()
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("Are you sure you want to exit?").setCancelable(
            false).setPositiveButton("Yes",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            }).setNegativeButton("No",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });
        AlertDialog alert = builder.create();
        alert.show();
     }

}

于 2012-11-26T08:04:04.247 回答