zoukankan      html  css  js  c++  java
  • 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.os.IBinder;
    import android.app.Activity;
    import android.content.ComponentName;
    import android.content.Intent;
    import android.content.ServiceConnection;
    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;
        private Button bindButton;
        private Button unbindButton;
        private ServiceConnection sConnection;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_ex0314);
            
             sConnection=new ServiceConnection() {
                
                @Override
                public void onServiceDisconnected(ComponentName name) {
                }
                
                @Override
                public void onServiceConnected(ComponentName name, IBinder service) {
                }
            };
            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);
                }
            });        
            
            bindButton=(Button)findViewById(id.Ex0314BindButton);
            bindButton.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);
                    bindService(sIntent,sConnection,BIND_AUTO_CREATE);
                }
            });        
            
            unbindButton=(Button)findViewById(id.Ex0314UnbindButton);
            unbindButton.setOnClickListener(new Button.OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    Intent sIntent=new Intent(EX0314.this,mService1.class);
                    sIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    unbindService(sConnection);
                }
            });            
        }
    
        @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 static final String TAG = "MyService";  
    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() {
        Log.e(TAG, "start onCreate~~~"); 
            super.onCreate();
            
            objHandler.postDelayed(mTasks, 1000);
        }
    @Override
        public void onDestroy() {
        Log.e(TAG, "start onDestroy~~~");  
            super.onDestroy();
            
            objHandler.removeCallbacks(mTasks);
        }
    @Override
        public IBinder onBind(Intent intent) {
        Log.e(TAG, "start IBinder~~~");  
            return null;
        }
    @Override
        public boolean onUnbind(Intent intent) {
        Log.e(TAG, "start onUnbind~~~");  
            return super.onUnbind(intent);
        }
    }
     
            <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>

  • 相关阅读:
    牛客练习赛24 E:青蛙(最短路)
    菜根谭#10
    菜根谭#9
    菜根谭#8
    菜根谭#7
    菜根谭#6
    菜根谭#5
    菜根谭#4
    菜根谭#3
    菜根谭#2
  • 原文地址:https://www.cnblogs.com/luquanmingren/p/4158124.html
Copyright © 2011-2022 走看看