2012-10-31 44 views
0

我正在創建一個應用程序來發送郵件。我正在使用擴展phoneStateListener類的類。這給出了startActivity函數時的問題,它說「方法startActivity(Intent)對於PhoneCallListener類型是未定義的」,其中PhoneCallListener是由phonestatelistener擴展的類,並將以下代碼寫入其中。如何從phoneStateListener類調用startActivity(intent)?

  String to = "[email protected]"; 
      String subject = "testing"; 
      String message = "this is it"; 
     Intent email = new Intent(Intent.ACTION_SEND); 
      email.putExtra(Intent.EXTRA_EMAIL, new String[]{ to}); 

      email.putExtra(Intent.EXTRA_SUBJECT, subject); 
      email.putExtra(Intent.EXTRA_TEXT, message); 

      //need this to prompts email client only 
      email.setType("message/rfc822"); 

       startActivity(Intent.createChooser(email, "Choose an Email client :")); 

請幫助我如何開始活動,以發送我的郵件。

+0

你還挺需要一個環境來開始一項活動。 – njzk2

回答

1
public class MyPhoneReceiver extends BroadcastReceiver { 
     Intent in; 
      @Override 
      public void onReceive(Context context, Intent intent) { 
       Bundle extras = intent.getExtras(); 
       if (extras != null) { 
       in = new Intent(context, Second.class); 
        in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
        context.startActivity(in); 

       } 
      } 
    } 

請確保您將接收器添加到清單文件中。

+0

是否需要顯式調用onReceive(),或者在情況相似時隱式完成? 如果是這樣,請告訴它在哪個條件下調用它的一些功能。 –

+0

是不是因爲應用程序收到郵件時調用onReceive方法? –

+0

最好在清單文件中明確地調用它。

相關問題