从内部服务启动应用程序

时间:2014-10-14 09:26:25

标签: java android

我有一项服务必须启动一个应用程序并每15秒计算一次。

我创建AlarmManager并每隔15秒调用一次服务。问题是当我按下启动按钮程序启动服务类并打开应用程序并开始计数时,但是当我按下停止按钮时,应用程序会收到错误< / p>

"Unfortunately, Program has stopped."

之后程序自动开始计算并打开应用程序 我无法阻止它。

我无法弄清楚。任何帮助或新的东西都可以做到这一点。感谢。

Myservice.java

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

public class Myservice extends Service{

    int counter=0;

    public IBinder onBind(Intent arg0) 
    {

        return null;
    }

    public void onCreate() {

        Toast.makeText(getApplicationContext(), "Service Created", Toast.LENGTH_SHORT).show();

    }

    @SuppressWarnings("deprecation")
    public void onStart(Intent intent, int startId) 
    {

          super.onStart(intent, startId);

          Startapp();

          counter++;

          Toast.makeText(getApplicationContext(), "Service Start : " + counter,Toast.LENGTH_SHORT).show();

    }


    public void onDestroy()
    {

          Toast.makeText(getApplicationContext(), "Service Destroy", Toast.LENGTH_SHORT).show();

          super.onDestroy();

    }


    public void Startapp()

    {   
        Intent in = getPackageManager().getLaunchIntentForPackage("com.example.application");
        startActivity(in);  

         Toast.makeText(getApplicationContext(), "Counter : " + counter, Toast.LENGTH_SHORT).show();
    }


}

MainActivity.java

import java.util.Calendar;
import java.util.List;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import android.widget.Toast;

public class MainActivity extends Activity {




     Intent myIntent;
     PendingIntent pendingIntent;
     AlarmManager alarmManager;

     Button btn1;
     Button btn2;



    @Override
    protected void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);




        btn1 = (Button)findViewById(R.id.button1);
        btn2 =(Button)findViewById(R.id.button2);




        btn1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {



                myIntent = new Intent(MainActivity.this, Clash.class);

                pendingIntent = PendingIntent.getService(MainActivity.this, 7778, myIntent, 0);

                alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

                Calendar calendar = Calendar.getInstance();

                calendar.setTimeInMillis(System.currentTimeMillis());

                calendar.add(Calendar.SECOND, 10);

                alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),15000, pendingIntent);

                Toast.makeText(MainActivity.this, "Start Alarm", Toast.LENGTH_LONG).show();

            }
        });


        btn2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                alarmManager.cancel(pendingIntent);
                pendingIntent.cancel();

                Toast.makeText(MainActivity.this, "Cancel!", Toast.LENGTH_LONG).show();
            }
        });



}

2 个答案:

答案 0 :(得分:0)

您需要设置此标志:

in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

因此,您的Startapp方法将如下所示:

public void Startapp()
{   
    Intent in = getPackageManager().getLaunchIntentForPackage("com.example.application");
    in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(in);  

     Toast.makeText(getApplicationContext(), "Counter : " + counter, Toast.LENGTH_SHORT).show();
}

答案 1 :(得分:0)

尝试使用它的工作....在停止按钮中设置...在创建后始终取消待处理的意图

 alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
    myIntent = new Intent(MainActivity.this, MyService.class);


    btn1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            pendingIntent = PendingIntent.getService(MainActivity.this, 7778, myIntent, 0);

            try {
                alarmManager.cancel(pendingIntent);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                System.out.println("ex....."+e);
            }

            Calendar calendar = Calendar.getInstance();

            calendar.setTimeInMillis(System.currentTimeMillis());

            calendar.add(Calendar.SECOND, 10);

            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),10000, pendingIntent);

            Toast.makeText(MainActivity.this, "Start Alarm", Toast.LENGTH_LONG).show();

        }
    });


    btn2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            pendingIntent = PendingIntent.getService(MainActivity.this, 7778, myIntent, 0);
            try {
                alarmManager.cancel(pendingIntent);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                System.out.println("ex....."+e);
            }
            MainActivity.this.stopService(myIntent);
            Toast.makeText(MainActivity.this, "Cancel!", Toast.LENGTH_LONG).show();
        }
    });