2012-03-27 65 views
-1

我正在開發一個android項目,我想創建一個攔截入站調用的應用程序。如何在聯繫人列表中分配一個複選框,以便能夠選擇多個聯繫人一次?顯示與複選框的聯繫人列表?

這裏是我的代碼:

//主要活動

public class MainActivity extends Activity { 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     add = (Button)findViewById(R.id.add_reminder); 
     manage = (Button)findViewById(R.id.manage_reminders); 
     add.setOnClickListener(this); 
     manage.setOnClickListener(this); 


    } 
    public void onClick(View v) { 
     switch(v.getId()) 
     { 
     case R.id.manage_reminders: 

      break; 
     case R.id.add_reminder: 
      Intent intent = new Intent(Intent.ACTION_PICK); 
       intent.setType(ContactsContract.Contacts.CONTENT_TYPE); 
       startActivityForResult(intent, PICK_CONTACT); 


      break; 

     } 


    } 
    public void onActivityResult(int requestCode, int resultCode, Intent intent) 
     { 

      if (requestCode == PICK_CONTACT) 
      {   
       Cursor cursor = managedQuery(intent.getData(), null, null, null, null); 
       cursor.moveToNext(); 
       String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 
       String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)); 

       Toast.makeText(this, "Contect LIST = "+name, Toast.LENGTH_LONG).show(); 
      } 
     }//onActivityResult 

} 

回答

0

看看這裏:http://www.krvarma.com/2010/08/detecting-incoming-and-outgoing-calls-in-android/

只是要偵聽的廣播接收器:

android.intent.action.PHONE_STATE 

如果電話狀態爲「振鈴」,則有來電。

TelephonyManager.EXTRA_STATE 

像:

public class IncomingCallReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
      Bundle bundle = intent.getExtras(); 

      if(null == bundle) 
        return; 

      String state = bundle.getString(TelephonyManager.EXTRA_STATE); 

      if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING)) 
      { 
        String phonenumber = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER); 

        Log.i("IncomingCallReceiver","Incoming Number: " + phonenumber); 
      } 
    } 
} 

現在的電話號碼將在logcat的打印。

+0

請提出一個簡單的代碼嗎? PLZ。我需要創建一個呼叫提醒。所以我需要在有人打電話時先打印電話號碼。 – user1222905 2012-03-27 08:26:44

+0

希望這有助於。您只需要註冊廣播接收器。隨着:RegisterReceiver(IncomingCallReceiver,* yourIntentFilter *) – 2012-03-27 08:34:35

+0

thx非常感謝您的幫助。我要去測試它。我怎樣才能將提醒提醒給一個號碼?我是否必須創建一個列表併爲每個號碼分配一條文本消息。當我有一個來電時,我只是用顯示的號碼發送消息? – user1222905 2012-03-27 08:40:34

相關問題