从传入的SMS中检测关键字并显示通知

时间:2012-07-19 17:13:06

标签: android notifications sms broadcastreceiver

您好我是Android的新手,我正在尝试从传入的短信中检测到关键字并在警报中显示通知,但我尝试了各种方法来使其工作,但它似乎不起作用。在Android模拟器2.2和我的Android手机4.0.4上尝试它并且它不起作用。代码中没有错误,我无法确定如何使其正常工作。

PhoneFinder.java

package com.example.phonefinder2;

import java.math.BigInteger;   
import java.security.MessageDigest;   
import java.security.NoSuchAlgorithmException;   

import android.app.Activity;   
import android.content.SharedPreferences.Editor;   
import android.os.Bundle;   
import android.util.Log;   
import android.view.View;   
import android.view.View.OnClickListener;   
import android.widget.Button;   
import android.widget.EditText;   
import android.widget.TextView;   

public class PhoneFinder extends Activity {   
    private EditText edit01;   
    private EditText edit02;   
    private Button button_ok;   
    private TextView textview;   
    static final String PASSWORD_PREF_KEY = "passwd";   
    /** Called when the activity is first created. */   
    @Override   
    public void onCreate(Bundle savedInstanceState) {   
        super.onCreate(savedInstanceState);   
        setContentView(R.layout.main);   

        edit01 = (EditText)findViewById(R.id.password);   
        edit02 = (EditText)findViewById(R.id.password_confirm);   
        textview = (TextView)findViewById(R.id.text1);   
        button_ok = (Button)findViewById(R.id.ok);   
        button_ok.setOnClickListener(listener);   
        /*SharedPreferences setting = getSharedPreferences(PASSWORD_PREF_KEY, 0);  
        String t1 = setting.getString("PASSWORD", null);  
        edit01.setText(t1);  
        edit02.setText(t1);*/   
    }   

    OnClickListener listener = new OnClickListener() {   
        public void onClick(View v) {   
            String p1 = edit01.getText().toString();   
            String p2 = edit02.getText().toString();   
            if(p1.equals(p2)) {   
                if(p1.length() >= 6 && p2.length() >= 6) {   
                    Editor edit = getSharedPreferences(PASSWORD_PREF_KEY, MODE_PRIVATE).edit();   
                    String md5hash = getMd5Hash(p1);   
                    edit.putString("PASSWORD", md5hash);   
                    edit.commit();   
                    textview.setText("password updated");   
                } else {   
                    textview.setText("password must be at least 6 characters");   
                }   
            } else {   
                edit01.setText("");   
                edit02.setText("");   
                textview.setText("password do not match");   
            }   
        }   
    };   

    public static String getMd5Hash(String input) {    
        try {    
            MessageDigest md = MessageDigest.getInstance("MD5");    
            byte[] messageDigest = md.digest(input.getBytes());    
            BigInteger number = new BigInteger(1,messageDigest);    
            String md5 = number.toString(16);    

            while (md5.length() < 32)    
                md5 = "0" + md5;    
                return md5;    
        } catch(NoSuchAlgorithmException e) {    
            Log.e("MD5", e.getMessage());    
            return null;    
        }    
    }    
}   

FinderReceiver.java

package com.example.phonefinder2;

import android.app.Notification;   
import android.app.NotificationManager;   
import android.app.PendingIntent;   
import android.content.BroadcastReceiver;   
import android.content.Context;   
import android.content.Intent;   
import android.content.SharedPreferences;   
import android.location.Location;   
import android.location.LocationManager;   
import android.os.Bundle;   
import android.telephony.SmsManager;   
import android.telephony.SmsMessage;   
import android.widget.Toast;

public class FinderReceiver extends BroadcastReceiver {

    Context context;   
    @Override    
    public void onReceive(Context context, Intent intent) {    
        this.context = context;   
        SharedPreferences passwdfile = context.getSharedPreferences(    
                PhoneFinder.PASSWORD_PREF_KEY, 0);    

        String correctMd5 = passwdfile.getString(PhoneFinder.PASSWORD_PREF_KEY,    
                null);    
        if (correctMd5 != null) {    

              Bundle bundle = intent.getExtras();   
              Object pdus[] = (Object[]) bundle.get("pdus");
              SmsMessage smsMessage[] = new SmsMessage[pdus.length];   

              for(int i = 0; i < smsMessage.length; i++) {   
                  smsMessage[i] = SmsMessage.createFromPdu((byte[])pdus[i]);   
                if (smsMessage[i].getMessageBody().contains("RING:")) {    
                    String[] tokens = smsMessage[i].getMessageBody().split(":");    
                    if (tokens.length >= 2) {    
                        String md5hash = PhoneFinder.getMd5Hash(tokens[1]);    

                        if (correctMd5.equals(md5hash)) {    
                            String to = smsMessage[i].getOriginatingAddress (); 
                            SmsManager sm = SmsManager.getDefault(); 

                             sm.sendTextMessage (to, null, "success!", null, null);
                            NotificationManager nm =  
                                (NotificationManager) context.getSystemService (Context.NOTIFICATION_SERVICE); 
                            Notification notification = new Notification(android.R.drawable.ic_dialog_info, "Alert", System.currentTimeMillis());
                            PendingIntent contentIntent = PendingIntent.getActivity (context, 0, new Intent (context, PhoneFinder.class), 0);
                            notification.setLatestEventInfo (context, "GPS address", "You have been detected", contentIntent);
                            nm.notify (R.string.service_start, notification);
                        }    
                    }    
                } }   
            //}    
        }   
    }    
}    

0 个答案:

没有答案