Poprosiłem o pomoc od was w sprawie programu i wygląda na to, że zadziała. czy mogę uruchomić wątki/asynktask. Próbowałem już pracować nad wątkami i nie udało mi się. Powodem, dla którego próbuję tworzyć wątki, jest to, że mój program ulega awarii po załadowaniu, a zasadniczo jego zadaniem jest wysłanie wiadomości e-mail, jeśli ktoś dzwoni, ponieważ zawsze zgubiłem telefon lub milczałem, a ja nie jestem tego świadomy.Czy ktoś może mi pomóc zrozumieć wątki do mojego programu?
Service-
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;
}
}
zadaniowego
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;
}
}
Dołącz swój LogCat. –
gotowe. http://pastebin.com/GnyKatuS – andyADD
@ user1884552: co jest naCreate (MainActivity.java:26) linii swojej działalności? –