gpt4 book ai didi

android - 任何人都可以帮助我了解我的程序的线程吗?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:48:02 25 4
gpt4 key购买 nike

我向你们求助了一个程序,看起来可以。如果我能让线程/异步任务工作。我已经尝试在线程上工作,但我没有让它工作。我尝试线程化的原因是因为我的程序在加载时崩溃了,基本上它应该做的是在有人打电话时发送电子邮件,因为我总是 Handlebars 机放错地方或保持静音,而且我我不知道。

服务-

    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;
}

}

Activity -

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;
}

}

最佳答案

将 MainActivity Activity 更改为从 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" />

关于android - 任何人都可以帮助我了解我的程序的线程吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13945469/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com