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[]{"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;
    }

}
4

1 回答 1

4

将 MainActivity 活动更改为从 Activity 启动服务:

    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" />
于 2012-12-19T04:59:18.897 回答