来自服务

时间:2016-11-04 05:27:08

标签: java android

之前已经问过几次,但提供的解决方案无法解决我的问题。我正在开发有几个类的应用程序:mainactivity,SMS和MService。服务有一个计时器。我试图在每次计时器结束时拨打短信发送短信。可以请有人帮助我.... 谢谢你考虑......

public class MService extends Service {

    private Handler HandleIt = new Handler();
    private final int INTERVAL = 60 * 1000;
    private Timer timer = new Timer();
    boolean timeout = false;


    public interface SmsService
    {
        void SmsServiceSenter();
    }



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





    class TimeDisplayTimerTask extends TimerTask {



        @Override
        public void run() {
            HandleIt.post(new Runnable(){
               public void run(){
                   Toast.makeText(getApplicationContext(), TextonScreen(), Toast.LENGTH_SHORT).show();
                  // Intent smsintent = new Intent(getBaseContext(), SMS.class);
                  // startService(smsintent);
               }
            });
        }


   }

    private String TextonScreen()
    {
        timeout = true;
        return "it is running";

    }
    boolean isTimeout()
    {
        return timeout;
    }



    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        Toast.makeText(this, "Service is created", Toast.LENGTH_SHORT).show();



    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub
        // Display the Toast Message
        Toast.makeText(this, "Start it", Toast.LENGTH_SHORT).show();
        // Execute an action after period time
        //comes from the TimeDisplayTimerTask class
        timer.scheduleAtFixedRate(new TimeDisplayTimerTask(), 0, INTERVAL);


        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        // Display the Toast Message
        Toast.makeText(this, "Stop it", Toast.LENGTH_SHORT).show();
        super.onDestroy();
    }


}

public class SMS extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        message();;


    }

    boolean issent = false;
    String text = "I am here";
    String num = "2085578209";
    SmsManager smsManager = SmsManager.getDefault();




    public void message()
    {
       // if(Timeout.isTimeout()) {
            smsManager.sendTextMessage(num, null, text, null, null);
            issent = true;
      //  }
    }
    boolean isSent()
    {
    return issent;
    }
}

1 个答案:

答案 0 :(得分:0)

这很简单。 在创建Intent变量之后,在开始活动之前添加一个标志,如下所示

Intent launch = new Intent(this, MyActivity.class);
launch.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(launch);

使用上面的代码,您可以从服务

调用活动
相关问题