任何人都可以帮我理解我的程序的线程吗?

时间:2012-12-19 04:23:55

标签: android multithreading

我向你们寻求一个程序的帮助,似乎它会起作用。如果我可以使线程/ asynctask工作。我已经尝试过使用线程,但我没有让它工作。我试图进行线程化的原因是因为我的程序在加载时崩溃了,基本上它假设要做的是发送电子邮件,如果有人正在打电话,因为我总是错放我的手机或保持静音,我我不知道。

服务 -

    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[]{"random@gmail.com"});
                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 个答案:

答案 0 :(得分: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" />