判断一个service是否启动

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/guai8023/article/details/51166430

public static boolean isWorked(String className) {  
ActivityManager myManager=(ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE); 
ArrayList<RunningServiceInfo> runningService = (ArrayList<RunningServiceInfo>) myManager.getRunningServices(30);  
for(int i = 0 ; i<runningService.size();i++)  {   
if(runningService.get(i).service.getClassName().toString().equals(className))   { 
   return true;   
}  }  
return false; 
}



展开阅读全文

启动多个service,如何知道哪个service是否启动

05-09

final boolean ServiceAOn = (PendingIntent.getService(_context, SERVICE_A_UNIQUE_ID,rnnew Intent(_context, ServiceA.class),rnPendingIntent.FLAG_NO_CREATE) != null);rn上面这个是我想知道SERVICE_A的状态,是否已经启动,但是第一次有用,第二次就没用了rnrnpackage com.example.test;rnrnimport java.text.SimpleDateFormat;rnimport java.util.Date;rnrnimport android.app.Activity;rnimport android.app.AlarmManager;rnimport android.app.PendingIntent;rnimport android.content.Context;rnimport android.content.Intent;rnimport android.os.Bundle;rnimport android.view.LayoutInflater;rnimport android.view.Menu;rnimport android.view.MenuItem;rnimport android.view.View;rnimport android.widget.Button;rnimport android.widget.LinearLayout;rnimport android.widget.RelativeLayout;rnimport android.widget.ScrollView;rnimport android.widget.TextView;rnimport android.widget.Toast;rnrnimport com.example.alarmmanagertest.R;rnimport com.example.service.ServiceA;rnimport com.example.service.ServiceB;rnimport com.example.service.ServiceC;rnrnpublic class MainActivity extends Activity rn private Context _context;rn private int SERVICE_A_TIME_INTERVAL = 10;rn private int SERVICE_B_TIME_INTERVAL = 20;rn private int SERVICE_C_TIME_INTERVAL = 30;rn private int SERVICE_A_UNIQUE_ID = 0;rn private int SERVICE_B_UNIQUE_ID = 1;rn private int SERVICE_C_UNIQUE_ID = 2;rn rn @Overridern protected void onCreate(Bundle savedInstanceState) rn super.onCreate(savedInstanceState);rn _context = this;rn setContentView(R.layout.activity_main);rn initView();rn rnrn private void initView() rn Button btn_start_alarm = (Button) findViewById(R.id.btn_start_alarm);rn btn_start_alarm.setOnClickListener(new View.OnClickListener() rn @Overridern public void onClick(View arg0) rn // Start all servicern fn_start_all_alarm();rn rn );rnrn Button btn_stop_alarm = (Button) findViewById(R.id.btn_stop_alarm);rn btn_stop_alarm.setOnClickListener(new View.OnClickListener() rn @Overridern public void onClick(View arg0) rn fn_cancel_all_alarm();rn rn );rnrn final boolean ServiceAOn = (PendingIntent.getService(_context, SERVICE_A_UNIQUE_ID,rn new Intent(_context, ServiceA.class),rn PendingIntent.FLAG_NO_CREATE) != null);rn Button btn_startOrStop_a = (Button) findViewById(R.id.btn_startOrStop_a);rn btn_startOrStop_a.setText(ServiceAOn == true ? "Stop Service A"rn : "Start Service A");rn btn_startOrStop_a.setOnClickListener(new View.OnClickListener() rn @Overridern public void onClick(View arg0) rn if (ServiceAOn == true) rn fn_cancel_a_service();rn fn_refresh_activity();rn else rn fn_start_a_alarm();rn fn_refresh_activity();rn rn rn );rn rn final boolean ServiceBOn = (PendingIntent.getService(_context, SERVICE_B_UNIQUE_ID,rn new Intent(_context, ServiceB.class),rn PendingIntent.FLAG_NO_CREATE) != null);rn Button btn_startOrStop_b = (Button) findViewById(R.id.btn_startOrStop_b);rn btn_startOrStop_b.setText(ServiceBOn == true ? "Stop Service B"rn : "Start Service B");rn btn_startOrStop_b.setOnClickListener(new View.OnClickListener() rn @Overridern public void onClick(View arg0) rn if (ServiceBOn == true) rn fn_cancel_b_service();rn fn_refresh_activity();rn else rn fn_start_b_alarm();rn fn_refresh_activity();rn rn rn );rn rn final boolean ServiceCOn = (PendingIntent.getService(_context, SERVICE_C_UNIQUE_ID,rn new Intent(_context, ServiceC.class),rn PendingIntent.FLAG_NO_CREATE) != null);rn Button btn_startOrStop_c = (Button) findViewById(R.id.btn_startOrStop_c);rn btn_startOrStop_c.setText(ServiceCOn == true ? "Stop Service C"rn : "Start Service C");rn btn_startOrStop_c.setOnClickListener(new View.OnClickListener() rn @Overridern public void onClick(View arg0) rn if (ServiceCOn == true) rn fn_cancel_c_service();rn fn_refresh_activity();rn else rn fn_start_c_alarm();rn fn_refresh_activity();rn rn rn );rn return;rn rnrn private void fn_start_all_alarm() rn fn_start_a_alarm();rn fn_start_b_alarm();rn fn_start_c_alarm();rn fn_refresh_activity();rn return;rn rn /*****Please note that the 1.AlarmManager and 2.PendingIntent can reuse during setting alarm*******/rn /*long now = System.currentTimeMillis();rn AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);rn PendingIntent pi = PendingIntent.getService(_context,rn SERVICE_A_UNIQUE_ID, new Intent(_context, ServiceA.class),rn Intent.FLAG_ACTIVITY_NEW_TASK);rn am.setInexactRepeating(AlarmManager.RTC_WAKEUP, now,rn 1000 * SERVICE_A_TIME_INTERVAL, pi);rn pi = PendingIntent.getService(_context, SERVICE_B_UNIQUE_ID,rn new Intent(_context, ServiceB.class),rn Intent.FLAG_ACTIVITY_NEW_TASK);rn am.setInexactRepeating(AlarmManager.RTC_WAKEUP, now,rn 1000 * SERVICE_B_TIME_INTERVAL, pi);rnrn pi = PendingIntent.getService(_context, SERVICE_C_UNIQUE_ID,rn new Intent(_context, ServiceC.class),rn Intent.FLAG_ACTIVITY_NEW_TASK);rn am.setInexactRepeating(AlarmManager.RTC_WAKEUP, now,rn 1000 * SERVICE_C_TIME_INTERVAL, pi);*/rnrnrn rnrn private void fn_cancel_all_alarm() rn fn_cancel_a_service();rn fn_cancel_b_service();rn fn_cancel_c_service();rn Toast.makeText(_context, "All service stopped", Toast.LENGTH_SHORT)rn .show();rn fn_refresh_activity();rn return;rn rn rn private void fn_start_a_alarm() rn long now = System.currentTimeMillis();rn AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);rn PendingIntent pi = PendingIntent.getService(_context,rn SERVICE_A_UNIQUE_ID, new Intent(_context, ServiceA.class),rn Intent.FLAG_ACTIVITY_NEW_TASK);rn am.setInexactRepeating(AlarmManager.RTC_WAKEUP, now,rn 1000 * SERVICE_A_TIME_INTERVAL, pi);rn fn_write_log("A alarm start");rn return;rn rn rn private void fn_start_b_alarm() rn long now = System.currentTimeMillis();rn AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);rn PendingIntent pi = PendingIntent.getService(_context,rn SERVICE_B_UNIQUE_ID, new Intent(_context, ServiceB.class),rn Intent.FLAG_ACTIVITY_NEW_TASK);rn am.setInexactRepeating(AlarmManager.RTC_WAKEUP, now,rn 1000 * SERVICE_B_TIME_INTERVAL, pi);rn fn_write_log("B alarm start");rn return;rn rn rn private void fn_start_c_alarm() rn long now = System.currentTimeMillis();rn AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);rn PendingIntent pi = PendingIntent.getService(_context,rn SERVICE_C_UNIQUE_ID, new Intent(_context, ServiceC.class),rn Intent.FLAG_ACTIVITY_NEW_TASK);rn am.setInexactRepeating(AlarmManager.RTC_WAKEUP, now,rn 1000 * SERVICE_C_TIME_INTERVAL, pi);rn fn_write_log("C alarm start");rn return;rn rn rn private void fn_refresh_activity() rn ((Activity) _context).finish();rn Intent intent = new Intent(_context, MainActivity.class);rn ((Activity) _context).startActivity(intent);rn rnrn private void fn_cancel_a_service() rn AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);rn PendingIntent ServiceAIntent = PendingIntent.getService(_context,rn SERVICE_A_UNIQUE_ID, new Intent(_context, ServiceA.class),rn Intent.FLAG_ACTIVITY_NEW_TASK);rn alarmManager.cancel(ServiceAIntent);rn fn_write_log("A alarm stop");rn return;rn rnrn private void fn_cancel_b_service() rn AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);rn PendingIntent ServiceBIntent = PendingIntent.getService(_context,rn SERVICE_B_UNIQUE_ID, new Intent(_context, ServiceB.class),rn Intent.FLAG_ACTIVITY_NEW_TASK);rn alarmManager.cancel(ServiceBIntent);rn fn_write_log("B alarm stop");rn return;rn rnrn private void fn_cancel_c_service() rn AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);rn PendingIntent ServiceCIntent = PendingIntent.getService(_context,rn SERVICE_C_UNIQUE_ID, new Intent(_context, ServiceC.class),rn Intent.FLAG_ACTIVITY_NEW_TASK);rn alarmManager.cancel(ServiceCIntent);rn fn_write_log("C alarm stop");rn return;rn rn rn private void fn_write_log(String log_msg) rn ScrollView scrollView = (ScrollView) findViewById(R.id.scrollView);rn rn View clone_row = getLayoutInflater().inflate(R.layout.clone_row, null);rnrn TextView datetime = (TextView) clone_row.findViewById(R.id.datetime);rn TextView serviceText = (TextView) clone_row.findViewById(R.id.serviceText);rn rn SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");rn String currentDateandTime = sdf.format(new Date());rn rn datetime.setText(currentDateandTime);rn serviceText.setText(log_msg);rn rn LinearLayout linlin_layout = (LinearLayout) scrollView.findViewById(R.id.linlin_layout);rn linlin_layout.addView(clone_row);rn return;rn rnrn @Overridern public boolean onCreateOptionsMenu(Menu menu) rn // Inflate the menu; this adds items to the action bar if it is present.rn return true;rn rnrn @Overridern public boolean onOptionsItemSelected(MenuItem item) rn // Handle action bar item clicks here. The action bar willrn // automatically handle clicks on the Home/Up button, so longrn // as you specify a parent activity in AndroidManifest.xml.rn return super.onOptionsItemSelected(item);rn rn 论坛

没有更多推荐了,返回首页