当前位置:  编程技术>移动开发
本页文章导读:
    ▪AsyncTask异步实现(便利自己后续复习)        AsyncTask异步实现(方便自己后续复习) public class Main extends Activity {         private LayoutInflater m_flater = null;       private LinearLayout mFlash;         public void onCreate(Bundle savedInstanceState) {   .........
    ▪ Services的更进一步测试(使用方法)        Services的进一步测试(使用方法) 1.界面操作类: Java代码    import android.app.Activity;   import android.content.ComponentName;   import android.content.Context;   import android.content.Intent;   import android.content.Se.........
    ▪ AsyncTask异步兑现(方便自己后续复习)       AsyncTask异步实现(方便自己后续复习) public class Main extends Activity {         private LayoutInflater m_flater = null;       private LinearLayout mFlash;         public void onCreate(Bundle savedInstanceState) {   .........

[1]AsyncTask异步实现(便利自己后续复习)
    来源: 互联网  发布时间: 2014-02-18
AsyncTask异步实现(方便自己后续复习)
public class Main extends Activity {  
 
    private LayoutInflater m_flater = null;  
    private LinearLayout mFlash;  
 
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.flash);  
        mFlash = (LinearLayout) findViewById(R.id.mFlash);  
        mFlash.startAnimation(AnimationUtils  
                .loadAnimation(this, R.anim.fadeout));  
        m_flater = getLayoutInflater();  
        LoadMainTask task = new LoadMainTask(this);  
        task.execute("");  
 
    }  
 
    public View LoadMainView(LayoutInflater flater) {  
        View view = flater.inflate(R.layout.main, null);  
        Button btnOk = (Button) view.findViewById(R.id.BtnOk);  
        btnOk.setOnClickListener(new OnClickListener() {  
            public void onClick(View v) {  
                finish();  
            }  
        });  
        return view;  
    }  
 
    private class LoadMainTask extends AsyncTask<Object, Object, View> {  
        public LoadMainTask(Context context) {  
        }  
 
        protected View doInBackground(Object... params) {  
            View view = null;  
            view = LoadMainView(m_flater);  
            // 为了测试加了延时,大家可以在这一块加载资源,数据等  
            try {  
                Thread.sleep(3000);  
            } catch (InterruptedException e) {  
                e.printStackTrace();  
            }  
            return view;  
        }  
 
        // 执行完毕  
        protected void onPostExecute(View view) {  
            setContentView(view);  
        }  
    }  
 


    
[2] Services的更进一步测试(使用方法)
    来源: 互联网  发布时间: 2014-02-18
Services的进一步测试(使用方法)
1.界面操作类:
Java代码 
 
import android.app.Activity;  
import android.content.ComponentName;  
import android.content.Context;  
import android.content.Intent;  
import android.content.ServiceConnection;  
import android.os.Bundle;  
import android.os.IBinder;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;  
import android.widget.Toast;  
 
public class TestServiceHolder extends Activity {  
    private boolean _isBound;  
    private TestService _boundService;  
 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
        setTitle("Service Test");  
 
        initButtons();  
 
    }  
      
    private ServiceConnection _connection = new ServiceConnection() {    
        public void onServiceConnected(ComponentName className, IBinder service) {             
            _boundService = ((TestService.LocalBinder)service).getService();    
                
            Toast.makeText(TestServiceHolder.this, "Service connected",    
                    Toast.LENGTH_SHORT).show();    
        }    
    
        public void onServiceDisconnected(ComponentName className) {    
            // unexpectedly disconnected,we should never see this happen.    
            _boundService = null;    
            Toast.makeText(TestServiceHolder.this, "Service connected",    
                    Toast.LENGTH_SHORT).show();    
        }    
    };    
      
    private void initButtons() {    
        Button buttonStart = (Button) findViewById(R.id.start_service);    
        buttonStart.setOnClickListener(new OnClickListener() {    
            public void onClick(View arg0) {    
                startService();    
            }    
        });    
    
        Button buttonStop = (Button) findViewById(R.id.stop_service);    
        buttonStop.setOnClickListener(new OnClickListener() {    
            public void onClick(View arg0) {    
                stopService();    
            }    
        });    
    
        Button buttonBind = (Button) findViewById(R.id.bind_service);    
        buttonBind.setOnClickListener(new OnClickListener() {    
            public void onClick(View arg0) {    
                bindService();    
            }    
        });    
    
        Button buttonUnbind = (Button) findViewById(R.id.unbind_service);    
        buttonUnbind.setOnClickListener(new OnClickListener() {    
            public void onClick(View arg0) {    
                unbindService();    
            }    
        });    
    }    
    
    private void startService() {    
        Intent i = new Intent(this, TestService.class);    
        this.startService(i);    
    }    
      
    private void stopService() {    
        Intent i = new Intent(this, TestService.class);    
        this.stopService(i);    
    }    
    
    private void bindService() {//调用onCreate(),onBind()方法  
        Intent i = new Intent(this, TestService.class);  
        bindService(i, _connection, Context.BIND_AUTO_CREATE);  
        _isBound = true;  
    }  
    
    private void unbindService() {    
        if (_isBound) {    
            unbindService(_connection);    
            _isBound = false;    
        }    
    }    



import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class TestServiceHolder extends Activity {
private boolean _isBound;
private TestService _boundService;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setTitle("Service Test");

initButtons();

}

    private ServiceConnection _connection = new ServiceConnection() { 
        public void onServiceConnected(ComponentName className, IBinder service) {          
            _boundService = ((TestService.LocalBinder)service).getService(); 
             
            Toast.makeText(TestServiceHolder.this, "Service connected", 
                    Toast.LENGTH_SHORT).show(); 
        } 
 
        public void onServiceDisconnected(ComponentName className) { 
            // unexpectedly disconnected,we should never see this happen. 
            _boundService = null; 
            Toast.makeText(TestServiceHolder.this, "Service connected", 
                    Toast.LENGTH_SHORT).show(); 
        } 
    }; 
   
    private void initButtons() { 
        Button buttonStart = (Button) findViewById(R.id.start_service); 
        buttonStart.setOnClickListener(new OnClickListener() { 
            public void onClick(View arg0) { 
                startService(); 
            } 
        }); 
 
        Button buttonStop = (Button) findViewById(R.id.stop_service); 
        buttonStop.setOnClickListener(new OnClickListener() { 
            public void onClick(View arg0) { 
                stopService(); 
            } 
        }); 
 
        Button buttonBind = (Button) findViewById(R.id.bind_service); 
        buttonBind.setOnClickListener(new OnClickListener() { 
            public void onClick(View arg0) { 
                bindService(); 
            } 
        }); 
 
        Button buttonUnbind = (Button) findViewById(R.id.unbind_service); 
        buttonUnbind.setOnClickListener(new OnClickListener() { 
            public void onClick(View arg0) { 
                unbindService(); 
            } 
        }); 
    } 
 
    private void startService() { 
        Intent i = new Intent(this, TestService.class); 
        this.startService(i); 
    } 
   
    private void stopService() { 
        Intent i = new Intent(this, TestService.class); 
        this.stopService(i); 
    } 
 
private void bindService() {//调用onCreate(),onBind()方法
Intent i = new Intent(this, TestService.class);
bindService(i, _connection, Context.BIND_AUTO_CREATE);
_isBound = true;
}
 
    private void unbindService() { 
        if (_isBound) { 
            unbindService(_connection); 
            _isBound = false; 
        } 
    } 
}


2.服务类:
Java代码 
import android.app.Notification;  
import android.app.NotificationManager;  
import android.app.PendingIntent;  
import android.app.Service;  
import android.content.Intent;  
import android.os.Binder;  
import android.os.IBinder;  
import android.util.Log;  
 
public class TestService extends Service {  
 
    private static final String TAG = "TestService";  
    private NotificationManager _nm;  
 
    @Override 
    public IBinder onBind(Intent i) {  
        Log.e(TAG, "============> TestService.onBind");  
        return null;  
    }  
 
    public class LocalBinder extends Binder {  
        TestService getService() {  
            return TestService.this;  
        }  
    }  
 
    @Override 
    public boolean onUnbind(Intent i) {  
        Log.e(TAG, "============> TestService.onUnbind");  
        return false;  
    }  
 
    @Override 
    public void onRebind(Intent i) {  
        Log.e(TAG, "============> TestService.onRebind");  
    }  
 
    @Override 
    public void onCreate() {// 服务第一次被启动时调用  
        Log.e(TAG, "============> TestService.onCreate");  
        _nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);  
        showNotification();  
    }  
 
    @Override//在onCreate()方法后被调用  
    public void onStart(Intent intent, int startId) {  
        Log.e(TAG, "============> TestService.onStart");  
    }  
 
    @Override 
    public void onDestroy() {//停止服务的时候被调用  
        _nm.cancel(R.string.service_started);  
        Log.e(TAG, "============> TestService.onDestroy");  
    }  
 
    private void showNotification() {  
        Notification notification = new Notification(R.drawable.face_1,  
                "Service started", System.currentTimeMillis());  
 
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,  
                new Intent(this, TestServiceHolder.class), 0);  
 
        // must set this for content view, or will throw a exception  
        notification.setLatestEventInfo(this, "Test Service",  
                "Service started", contentIntent);  
 
        _nm.notify(R.string.service_started, notification);  
    }  
 


import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

public class TestService extends Service {

private static final String TAG = "TestService";
private NotificationManager _nm;

@Override
public IBinder onBind(Intent i) {
Log.e(TAG, "============> TestService.onBind");
return null;
}

public class LocalBinder extends Binder {
TestService getService() {
return TestService.this;
}
}

@Override
public boolean onUnbind(Intent i) {
Log.e(TAG, "============> TestService.onUnbind");
return false;
}

@Override
public void onRebind(Intent i) {
Log.e(TAG, "============> TestService.onRebind");
}

@Override
public void onCreate() {// 服务第一次被启动时调用
Log.e(TAG, "============> TestService.onCreate");
_nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
showNotification();
}

@Override//在onCreate()方法后被调用
public void onStart(Intent intent, int startId) {
Log.e(TAG, "============> TestService.onStart");
}

@Override
public void onDestroy() {//停止服务的时候被调用
_nm.cancel(R.string.service_started);
Log.e(TAG, "============> TestService.onDestroy");
}

private void showNotification() {
Notification notification = new Notification(R.drawable.face_1,
"Service started", System.currentTimeMillis());

PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, TestServiceHolder.class), 0);

// must set this for content view, or will throw a exception
notification.setLatestEventInfo(this, "Test Service",
"Service started", contentIntent);

_nm.notify(R.string.service_started, notification);
}

}



3.配置文件:
Java代码 
<application android:icon="@drawable/icon" android:label="@string/app_name">  
        <activity android:name=".TestServiceHolder" 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:enabled="true" android:name=".TestService" 
            android:process=":remote" />  
 
    </application> 

    
[3] AsyncTask异步兑现(方便自己后续复习)
    来源: 互联网  发布时间: 2014-02-18
AsyncTask异步实现(方便自己后续复习)
public class Main extends Activity {  
 
    private LayoutInflater m_flater = null;  
    private LinearLayout mFlash;  
 
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.flash);  
        mFlash = (LinearLayout) findViewById(R.id.mFlash);  
        mFlash.startAnimation(AnimationUtils  
                .loadAnimation(this, R.anim.fadeout));  
        m_flater = getLayoutInflater();  
        LoadMainTask task = new LoadMainTask(this);  
        task.execute("");  
 
    }  
 
    public View LoadMainView(LayoutInflater flater) {  
        View view = flater.inflate(R.layout.main, null);  
        Button btnOk = (Button) view.findViewById(R.id.BtnOk);  
        btnOk.setOnClickListener(new OnClickListener() {  
            public void onClick(View v) {  
                finish();  
            }  
        });  
        return view;  
    }  
 
    private class LoadMainTask extends AsyncTask<Object, Object, View> {  
        public LoadMainTask(Context context) {  
        }  
 
        protected View doInBackground(Object... params) {  
            View view = null;  
            view = LoadMainView(m_flater);  
            // 为了测试加了延时,大家可以在这一块加载资源,数据等  
            try {  
                Thread.sleep(3000);  
            } catch (InterruptedException e) {  
                e.printStackTrace();  
            }  
            return view;  
        }  
 
        // 执行完毕  
        protected void onPostExecute(View view) {  
            setContentView(view);  
        }  
    }  
 


    
最新技术文章:
▪Android开发之登录验证实例教程
▪Android开发之注册登录方法示例
▪Android获取手机SIM卡运营商信息的方法
▪Android实现将已发送的短信写入短信数据库的...
▪Android发送短信功能代码
▪Android根据电话号码获得联系人头像实例代码
▪Android中GPS定位的用法实例
linux iis7站长之家
▪Android实现文件的分割和组装
▪Android录音应用实例教程
▪Android双击返回键退出程序的实现方法
▪Android实现侦听电池状态显示、电量及充电动...
▪Android获取当前已连接的wifi信号强度的方法
▪Android实现动态显示或隐藏密码输入框的内容
▪根据USER-AGENT判断手机类型并跳转到相应的app...
▪Android Touch事件分发过程详解
▪Android中实现为TextView添加多个可点击的文本
▪Android程序设计之AIDL实例详解
▪Android显式启动与隐式启动Activity的区别介绍
▪Android按钮单击事件的四种常用写法总结
▪Android消息处理机制Looper和Handler详解
▪Android实现Back功能代码片段总结
▪Android实用的代码片段 常用代码总结
▪Android实现弹出键盘的方法
▪Android中通过view方式获取当前Activity的屏幕截...
▪Android提高之自定义Menu(TabMenu)实现方法
▪Android提高之多方向抽屉实现方法
▪Android提高之MediaPlayer播放网络音频的实现方法...
▪Android提高之MediaPlayer播放网络视频的实现方法...
▪Android提高之手游转电视游戏的模拟操控
 


站内导航:


特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

©2012-2021,,E-mail:www_#163.com(请将#改为@)

浙ICP备11055608号-3