android 入门-Service

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>

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。