Android初学之使用IntentService或Service获取当前时间

本文介绍了一个简单的Android应用程序示例,展示了如何利用IntentService来执行后台任务,如记录当前时间。通过MainActivity启动AndServer服务,该服务继承自IntentService,能够执行长时间运行的操作而不阻塞UI线程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 写Activity【实例是打印当前的时间】

1) 容易错的地方

   是

startService
而不是
startActivity

package com.mingrisoft;

import android.app.IntentService;
import android.content.Intent;
import android.text.format.Time;
import android.util.Log;

public class AndServer extends IntentService{

	public AndServer() {
		super("设置一下当前时间的服务");
	}

	@Override
	protected void onHandleIntent(Intent intent) {
		Time time = new Time();
		time.setToNow();
		String timesString = time.format("%Y-%m-%d %H:%M:%S");
		Log.i("std", timesString);
	}

}





package com.mingrisoft;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.Toast;

/**
 * Demonstration of using fragments to implement different activity layouts.
 * This sample provides a different layout (and activity flow) when run in
 * landscape.
 */
public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main); // 设置该Activity使用的布局
		ImageView iv=(ImageView)findViewById(R.id.imageButton0);
		iv.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Toast.makeText(MainActivity.this, "正准备进入游戏...", Toast.LENGTH_SHORT).show();
				startService(new Intent(MainActivity.this,AndServer.class));
//				startActivity(new Intent(MainActivity.this,CurrentTimeService.class));
//                startService(new Intent(MainActivity.this, CurrentTimeService.class));// 启动服务
			}
		});
	}
}

2一个IntentService类

package com.mingrisoft;

import android.app.IntentService;
import android.content.Intent;
import android.text.format.Time;
import android.util.Log;

public class AndServer extends IntentService{

	public AndServer() {
		super("设置一下当前时间的服务");
	}

	@Override
	protected void onHandleIntent(Intent intent) {
		Time time = new Time();
		time.setToNow();
		String timesString = time.format("%Y-%m-%d %H:%M:%S");
		Log.i("std", timesString);
	}

}

 需要注意 构造方法重写用不带参数的

3 在 配置文件加上

    <service android:name=".AndServer" >
        </service>

4 结果


5 还可以是用Service,都类似,Service主题类如下,结果还是一样的

package com.mingrisoft;

import android.app.IntentService;
import android.content.Intent;
import android.text.format.Time;
import android.util.Log;

public class AndServer extends IntentService{

	public AndServer() {
		super("设置一下当前时间的服务");
	}

	@Override
	protected void onHandleIntent(Intent intent) {
		Time time = new Time();
		time.setToNow();
		String timesString = time.format("%Y-%m-%d %H:%M:%S");
		Log.i("std", timesString);
	}

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

静山晚风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值