如何在创建新实例时完成以前的服务

时间:2015-09-17 07:34:19

标签: android eclipse service android-service development-environment

每当创建Main_Activity时,服务也会创建,但之前的服务都没有完成我想在创建新服务时完成旧服务
这是我的主要课程,我在创建主要活动时将其称为“我的服务”

Main_Activity Class

        package com.example.pushtrial;

        import android.app.Activity;
        import android.content.Intent;
        import android.os.Bundle;
        import android.view.Menu;
        import android.view.MenuItem;

        public class MainActivity extends Activity {

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);


            }

            @Override
            public void onDestroy()
            {
                super.onDestroy();
                //stopService(this.getIntent());
                Intent intent = new Intent(this, MyService.class);
                startService(intent);

            }
            @Override
            public boolean onCreateOptionsMenu(Menu menu) {
                // Inflate the menu; this adds items to the action bar if it is present.
                getMenuInflater().inflate(R.menu.main, menu);
                return true;
            }

            @Override
            public boolean onOptionsItemSelected(MenuItem item) {
                // Handle action bar item clicks here. The action bar will
                // automatically handle clicks on the Home/Up button, so long
                // as you specify a parent activity in AndroidManifest.xml.
                int id = item.getItemId();
                if (id == R.id.action_settings) {
                    return true;
                }
                return super.onOptionsItemSelected(item);
            }
        }

这是我的服务类:

package com.example.pushtrial;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.CountDownTimer;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class MyService extends Service {
    private static final String TAG = "MyService";
    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
    @Override
    public void onCreate() {

        super.onCreate();
        Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onCreate");
    }



    @Override
    public void onStart(Intent intent, int startid) {


        Log.d(TAG, "onStart");
        //new Timer().schedule(func(), "5000");// 24 hours ----> 86400
        new CountDownTimer(30000, 1000) {

             public void onTick(long millisUntilFinished) {
                 Log.d(TAG, "seconds remaining: " + millisUntilFinished / 1000);
                 //mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
             }

             public void onFinish() {
                 Log.d(TAG, "Done!");
                 func();
             }
          }.start();
        //new Timer().schedule(func(),120000);
        //player.start();
    }
    public void func()
    {
        Intent myIntent = new Intent(this, SecondActivity.class);//MainActivity.class
        myIntent.putExtra("FromNotification", true);
        PendingIntent myPendingIntent = PendingIntent.getActivity(this, 0, myIntent, 0);

        Notification myNotification  = new Notification.Builder(this)
            .setContentTitle("Event App")
            .setContentText("Hey, [Event App] is missing you! Got a minute?")
            .setSmallIcon(R.drawable.icon)
            .setContentIntent(myPendingIntent)
            .setAutoCancel(true)
            .build();//.addAction(R.drawable.icon, "Start Service", null)myPendingIntent

        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        notificationManager.notify(0, myNotification); 


    }
}

SecondActivity.class

package com.example.pushtrial;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class SecondActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.second, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

1 个答案:

答案 0 :(得分:0)

正如@pskink所说,你没有MyService的2个实例。会发生什么是在同一个实例上多次调用onStart()

如果您想阻止并行运行多个CountDownTimer,则需要向Service添加代码以跟踪当前正在运行的CountDownTimer并将其删除并且如果已经有一个请求,则忽略第二个请求启动一个请求。