2012-12-19 158 views
5

我向你們求助了一個程序,看起來它會起作用。如果我可以讓線程/異步應用程序工作。我試圖在線程上工作,但我沒有得到它的工作。我嘗試進行線程化的原因是因爲我的程序在加載時崩潰,基本上它應該做的是在有人打電話時發送電子郵件,因爲我總是放錯了手機或保持沉默,而我我沒有意識到這一點。任何人都可以幫助我理解我的程序的線程嗎?

服務 -

public class Callservice extends Service { 

    PhoneStateListener listener; 
    TelephonyManager tm; 

    @Override 
    public void onCreate() 
    { 
      Toast.makeText(getApplicationContext(), "Service has started", Toast.LENGTH_LONG).show(); 
      listener = new MyphoneStateListener(); 
      tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE); 
      tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE); 



    } 

     public class MyphoneStateListener extends PhoneStateListener 
     { 
      public void onCallStateChanged(int state, String incomingNumber) 
      { 
       switch (state) { 
        case TelephonyManager.CALL_STATE_IDLE: 
         break; 
        case TelephonyManager.CALL_STATE_RINGING: 
         sendemail(); 
         break; 
        case TelephonyManager.CALL_STATE_OFFHOOK: 
        default: 
         break; 
        } 
        super.onCallStateChanged(state, incomingNumber); 
       } 
      } 

      private void sendemail() 
      { 
       Intent i = new Intent(Intent.ACTION_SEND); 
       i.setType("message/rfc822"); 
       i.putExtra(Intent.EXTRA_EMAIL , new String[]{"[email protected]"}); 
       i.putExtra(Intent.EXTRA_SUBJECT , "phone is ringing"); 
       i.putExtra(Intent.EXTRA_TEXT , "email successfully sent"); 
       startActivity(Intent.createChooser(i, "send mail...")); 
       Toast.makeText(getApplicationContext(), "Email has been sent", Toast.LENGTH_LONG).show(); 

        try 
        { 
         startActivity(Intent.createChooser(i, "Send Email...")); 
        } 
        catch(android.content.ActivityNotFoundException ex) 
        { 

        } 
      } 


    @Override 
    public IBinder onBind(Intent arg0) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

} 

活動 -

public class MainActivity extends Activity { 


    Callservice callservice = new Callservice(); 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     callservice.onCreate(); 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 

} 
+1

附加您的LogCat。 –

+0

在這裏雅去。 http://pastebin.com/GnyKatuS – andyADD

+0

@ user1884552:什麼是onCreate(MainActivity.java:26)行你的活動? –

回答

4

變化MainActivity活動作爲從活動開始服務:

public class MainActivity extends Activity { 

    //Callservice callservice = new Callservice(); 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     startService(new Intent(this,Callservice.class)); 
    } 
    // your code here... 

,並確保你有將您的服務添加到AndroidManifest.xml中:

<service android:name=".Callservice" /> 
相關問題