2012-09-05 44 views
1

我使用的是类SMSBroadcastReceiver非常的时间,我收到短信在我的压延机创建一个事件的代码是这样的方法startActivity(意向)是未定义的类型SMSBroadcastReceiver

package com.arnaldo.smscal; 

    import java.util.GregorianCalendar; 

    import android.content.BroadcastReceiver; 
    import android.content.ContentResolver; 
    import android.content.ContentUris; 
    import android.content.Context; 
    import android.content.Intent; 
    import android.content.SharedPreferences; 

import android.database.Cursor; 
import android.net.Uri; 
import android.os.Bundle; 
import android.provider.CalendarContract; 
import android.provider.Contacts; 
import android.provider.CalendarContract.Events; 
import android.provider.Contacts.People; 
import android.provider.Contacts.Phones; 
import android.provider.ContactsContract; 
import android.provider.ContactsContract.PhoneLookup; 
import android.telephony.SmsMessage; 
import android.util.Log; 
import android.widget.EditText; 
import android.widget.Toast; 
import android.app.Activity; 
import android.view.Menu; 
import android.view.View; 
import android.widget.Button; 


public class SMSBroadcastReceiver extends BroadcastReceiver{ 



    private static final ContentResolver ContentResolver = null; 
    private static final Context Context = null; 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     // TODO Auto-generated method stub 

     if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){ 
      Bundle bundle = intent.getExtras();   //---get the SMS message passed in--- 
      SmsMessage[] msgs = null; 
      String msg_from; 




      if (bundle != null){ 
       //---retrieve the SMS message received--- 
       try{ 
        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]); 
         msg_from = msgs[i].getOriginatingAddress(); 
         String msgBody = msgs[i].getMessageBody(); 
         Log.e(msg_from, msgBody); 


     String contact; 
         Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(msg_from)); 
         Cursor cs= context.getContentResolver().query(uri, new String[]{PhoneLookup.DISPLAY_NAME},PhoneLookup.NUMBER+"='"+msg_from+"'",null,null); 

         if(cs.getCount()>0) 
         { 
         cs.moveToFirst(); 
         contact=cs.getString(cs.getColumnIndex(PhoneLookup.DISPLAY_NAME)); 
         Log.e("Foi de", contact); 


         //------------// 

         Intent calIntent = new Intent(Intent.ACTION_INSERT); 
         calIntent.setType("vnd.android.cursor.item/event");  
         calIntent.putExtra(Events.TITLE, "My House Party"); 
         calIntent.putExtra(Events.EVENT_LOCATION, "My Beach House"); 
         calIntent.putExtra(Events.DESCRIPTION, "A Pig Roast on the Beach"); 

         GregorianCalendar calDate = new GregorianCalendar(2012, 9, 5); 
         calIntent.putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY, true); 
         calIntent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, calDate.getTimeInMillis()); 
         calIntent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, calDate.getTimeInMillis()); 

         startActivity(calIntent); 


         //------------// 


         } 


        } 
       }catch(Exception e){ 
          Log.w("Exception caught",e.getMessage()); 
       } 
      } 
     } 
    } 
} 

问题是startActivity(calIntent);
我有错误“的方法startActivity(意向)是未定义的类型SMSBroadcastReceiver”

,因为它被传递到的onReceive函数请帮助

+0

而的answere被发现在哪里:http://stackoverflow.com/questions/6468463/start-activity-inside-onreceive-broadcastreceiver – user828310

回答

0

使用情境:

 context.startActivity(intent); 
相关问题