如何在Android应用中运行动态服务?

时间:2014-08-19 19:11:39

标签: android dynamic service

我正在使用Android代码编写代码,以便在Android应用中创建动态服务或运行时服务。我想创建一个服务,它将在内部运行两个动态服务,具有唯一ID。但我无法弄清楚如何做到这一点?请查看我的示例代码:

mycode的:

MainActivity.java

package com.example.multipleservice;

import java.util.Calendar;
import java.util.concurrent.TimeUnit;


import android.os.Bundle;
import android.os.CountDownTimer;
import android.app.Activity; 
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.SharedPreferences;
import android.widget.TextView;

public class MainActivity extends Activity 
{

TextView timer1;

TextView timer2;


@Override
protected void onCreate(Bundle savedInstanceState) 
{


    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    timer1 = (TextView) findViewById(R.id.timer1);

    timer2 = (TextView) findViewById(R.id.timer2);




     Calendar calendar1 = Calendar.getInstance();

     Intent myIntent1 = new Intent(MainActivity.this, MyService.class);

     myIntent1.putExtra("Id", "1");

     PendingIntent pendingIntent1 = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent1,0);

     AlarmManager alarmManager1 = (AlarmManager)getSystemService(ALARM_SERVICE);

     alarmManager1.set(AlarmManager.RTC, calendar1.getTimeInMillis(), pendingIntent1);



     long tm1 = 0;

     String ticket1 = "1";

     SharedPreferences timer_saved1 = getApplicationContext().getSharedPreferences("TimerSave"+ticket1, 0);

     long timerData1 = timer_saved1.getLong("timer"+ticket1, 0);

     long timerValue1 = 0;

     long diff_value1 = 36680;

     if(timerData1!=0)
     {

        timerValue1 = timerData1*1000;

     }
     else
     {

        timerValue1 = diff_value1*1000;

     }


     tm1 = timerValue1;

     RemainTime1 timera = new RemainTime1(tm1,1000);

     timera.start();        





     Calendar calendar2 = Calendar.getInstance();

     Intent myIntent2 = new Intent(MainActivity.this, MyService.class);

     myIntent2.putExtra("Id", "2");

     PendingIntent pendingIntent2 = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent2,0);

     AlarmManager alarmManager2 = (AlarmManager)getSystemService(ALARM_SERVICE);

     alarmManager2.set(AlarmManager.RTC, calendar2.getTimeInMillis(), pendingIntent2);



     long tm2 = 0;

     String ticket2 = "2";

     SharedPreferences timer_saved2 = getApplicationContext().getSharedPreferences("TimerSave"+ticket2, 0);

     long timerData2 = timer_saved2.getLong("timer"+ticket2, 0);

     long timerValue2 = 0;

     long diff_value2 = 36880;

     if(timerData2!=0)
     {

        timerValue2 = timerData2*1000;

     }
     else
     {

        timerValue2 = diff_value2*1000;

     }


     tm2 = timerValue2;

     RemainTime2 timerb = new RemainTime2(tm2,1000);

     timerb.start();





}


public class RemainTime1 extends CountDownTimer 
{

    public RemainTime1(long millisInFuture, long countDownInterval) 
    {

        super(millisInFuture, countDownInterval);

    }

    @Override
    public void onFinish()
    {



    }

    @Override
    public void onTick(long arg0) 
    {


        long millis = arg0;

        long hour = TimeUnit.MILLISECONDS.toHours(millis);

        long min = TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis));

        long sec = TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis));

        long hr = 0;

        if(hour>=24)
        {

            hr = hour - 24*7;

        }
        else
        {

            hr = hour;

        }

        timer1.setText(hr+" hr : "+min+" mins : "+sec+" sec ");



    }


}



public class RemainTime2 extends CountDownTimer 
{

    public RemainTime2(long millisInFuture, long countDownInterval) 
    {

        super(millisInFuture, countDownInterval);

    }

    @Override
    public void onFinish()
    {



    }

    @Override
    public void onTick(long arg0) 
    {


        long millis = arg0;

        long hour = TimeUnit.MILLISECONDS.toHours(millis);

        long min = TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis));

        long sec = TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis));

        long hr = 0;

        if(hour>=24)
        {

            hr = hour - 24*7;

        }
        else
        {

            hr = hour;

        }

        timer2.setText(hr+" hr : "+min+" mins : "+sec+" sec ");


    }


}}

MySerivce.java

package com.example.multipleservice;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class MyService extends BroadcastReceiver
{

  @Override
  public void onReceive(Context context, Intent intent)
  {

    Intent service1 = new Intent(context, MyAlarmService.class);

    context.startService(service1);

} }

MyAlarmService.java

package com.example.multipleservice;

import java.util.concurrent.TimeUnit;

import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.CountDownTimer;
import android.os.IBinder;
import android.util.Log;
import android.widget.Chronometer;
import android.widget.Toast;


 public class MyAlarmService extends Service 
 {




@Override
public IBinder onBind(Intent intent) 
{

    return null;

}

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


            String ticket = intent.getStringExtra("Id");


            SharedPreferences timer_saved = getApplicationContext().getSharedPreferences("TimerSave"+ticket, 0);

            long timerData = timer_saved.getLong("timer"+ticket, 0);

            long timerValue = 0;

            long diff_value = 0;

            if(timerData!=0)
            {

                timerValue = timerData*1000;

            }
            else
            {

                timerValue = diff_value*1000;

            }



            RemainTime timer = new RemainTime(timerValue,1000);

            timer.start(); 



    super.onStart(intent, startId);

}



public class RemainTime extends CountDownTimer 
{


    public RemainTime(long millisInFuture, long countDownInterval) 
    {

        super(millisInFuture, countDownInterval);

    }

    @Override
    public void onFinish()
    {



    }

    @Override
    public void onTick(long arg0) 
    {


        long millis = arg0;

        long hour = TimeUnit.MILLISECONDS.toHours(millis);

        long min = TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis));

        long sec = TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis));

        long hr = 0;

        if(hour>=24)
        {

            hr = hour - 24*7;

        }
        else
        {

            hr = hour;

        }



    }


} }

这是我上面的代码,我在 getStringExtra 部分得到 NullPointerException 。我想创建两个不同的计时器,当我关闭应用程序时,但仍然应该在服务中运行两个计时器,当我再次打开应用程序时,我应该从两个时钟看到更新的计时器值。

请帮帮我!!!请建议我一些可能的解决方案。

0 个答案:

没有答案