码迷,mamicode.com
首页 > 移动开发 > 详细

android 入门-Service

时间:2014-12-11 18:52:21      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:des   android   style   blog   http   io   ar   color   os   

sdk 1.7

package com.example.hellowrold;

import java.util.Random;

import com.example.hellowrold.R.id;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class EX0314 extends Activity {

    private Button startButton;
    private Button stopbuButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ex0314);
        
        startButton=(Button)findViewById(id.Ex0314StartButton);
        startButton.setOnClickListener(new Button.OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent sIntent=new Intent(EX0314.this,mService1.class);
                sIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startService(sIntent);
            }
        });        
        
        stopbuButton=(Button)findViewById(id.Ex0314StopButton);
        stopbuButton.setOnClickListener(new Button.OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent sIntent=new Intent(EX0314.this,mService1.class);
                stopService(sIntent);
            }
        });        
    }

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

}
package com.example.hellowrold;

import android.R.integer;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;

public class mService1 extends Service{
private Handler objHandler=new Handler();
private int intCounter=0;
private Runnable mTasks=new Runnable() {
    
    @Override
    public void run() {
        // TODO Auto-generated method stub
        intCounter++;
        Log.i("hippo","计算器"+Integer.toString(intCounter));
        objHandler.postDelayed(mTasks, 1000);
    }
};

@Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        
        objHandler.postDelayed(mTasks, 1000);
    }
@Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        
        objHandler.removeCallbacks(mTasks);
    }
@Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
}
        <activity
            android:name="com.example.hellowrold.EX0314"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".mService1"
            android:exported="false">
        </service>

bubuko.com,布布扣bubuko.com,布布扣bubuko.com,布布扣

android 入门-Service

标签:des   android   style   blog   http   io   ar   color   os   

原文地址:http://www.cnblogs.com/luquanmingren/p/4158124.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!