編輯:關於Android編程
package com.xjp.broadcast; import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { private final static String action = "com.xjp.MainActivity"; private TextView result; private Button startService; private Button stopService; private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { int i = intent.getIntExtra("key", 0); result.setText(i + ""); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); result = (TextView) findViewById(R.id.result); startService = (Button) findViewById(R.id.startService); startService.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent service = new Intent(MainActivity.this, CalculationService.class); startService(service); } }); stopService = (Button) findViewById(R.id.stopService); stopService.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent service = new Intent(MainActivity.this, CalculationService.class); stopService(service); } }); } @Override protected void onResume() { super.onResume(); initFilter(); } @Override protected void onDestroy() { super.onDestroy(); unregisterReceiver(broadcastReceiver); } private void initFilter() { IntentFilter filter = new IntentFilter(); filter.addAction(action); registerReceiver(broadcastReceiver, filter); } }
package com.xjp.broadcast; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.util.Log; /** * Description: * User: xjp * Date: 2015/5/4 * Time: 14:14 */ public class CalculationService extends Service { private final static String TAG = "CalculationService"; private final static String action = "com.xjp.MainActivity"; private boolean quit = false; @Override public IBinder onBind(Intent intent) { Log.e(TAG, "====onBind====="); return null; } @Override public boolean onUnbind(Intent intent) { Log.e(TAG, "====onUnbind====="); return super.onUnbind(intent); } @Override public void onCreate() { Log.e(TAG, "====onCreate====="); super.onCreate(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.e(TAG, "====onStartCommand====="); startThread(); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { Log.e(TAG, "====onDestroy====="); quit = true; super.onDestroy(); } /** * 開啟線程模擬耗時任務 */ public void startThread() { new Thread(new Runnable() { int i = 0; @Override public void run() { while (i < 200 && !quit) { try { Thread.sleep(1000); i++; Intent intent = new Intent(action); intent.putExtra("key", i); sendBroadcast(intent); } catch (InterruptedException e) { e.printStackTrace(); } } } }).start(); } }
package com.xjp.broadcast; import android.app.Activity; import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { private final static String action = "com.xjp.MainActivity"; private TextView result; private Button startService; private Button stopService; private CalculationService calculationService; private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { int i = intent.getIntExtra("key", 0); result.setText(i + ""); } }; private ServiceConnection serviceConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { calculationService = ((CalculationService.CalulationBinder) service).getService(); calculationService.startThread(); } @Override public void onServiceDisconnected(ComponentName name) { calculationService = null; } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); result = (TextView) findViewById(R.id.result); startService = (Button) findViewById(R.id.startService); startService.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent service = new Intent(MainActivity.this, CalculationService.class); bindService(service, serviceConnection, Context.BIND_AUTO_CREATE); } }); stopService = (Button) findViewById(R.id.stopService); stopService.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { unbindService(serviceConnection); } }); } @Override protected void onResume() { super.onResume(); initFilter(); } @Override protected void onDestroy() { super.onDestroy(); unregisterReceiver(broadcastReceiver); } private void initFilter() { IntentFilter filter = new IntentFilter(); filter.addAction(action); registerReceiver(broadcastReceiver, filter); } }
package com.xjp.broadcast; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; import android.util.Log; /** * Description: * User: xjp * Date: 2015/5/4 * Time: 14:14 */ public class CalculationService extends Service { private final static String TAG = "CalculationService"; private final static String action = "com.xjp.MainActivity"; private boolean quit = false; private IBinder binder = new CalulationBinder(); public class CalulationBinder extends Binder { public CalculationService getService() { return CalculationService.this; } } @Override public IBinder onBind(Intent intent) { Log.e(TAG, "====onBind====="); return binder; } @Override public boolean onUnbind(Intent intent) { Log.e(TAG, "====onUnbind====="); return super.onUnbind(intent); } @Override public void onCreate() { Log.e(TAG, "====onCreate====="); super.onCreate(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.e(TAG, "====onStartCommand====="); startThread(); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { Log.e(TAG, "====onDestroy====="); quit = true; super.onDestroy(); } /** * 開啟線程模擬耗時任務 */ public void startThread() { new Thread(new Runnable() { int i = 0; @Override public void run() { while (i < 200 && !quit) { try { Thread.sleep(1000); i++; Intent intent = new Intent(action); intent.putExtra("key", i); sendBroadcast(intent); } catch (InterruptedException e) { e.printStackTrace(); } } } }).start(); } }
一次點擊“啟動服務”按鈕和 “停止服務” 按鈕,生命周期 打印結果如下:
本文實例演示如何從圖庫(Gallery)中讀取圖像並用ImageView將它顯示出來,供大家參考,具體內容如下運行本示例前,需要先利用相機模擬拍攝一些圖片到圖庫中。1、運
這篇文章是繼自定義EditText樣式之後的功能強化,對於實際應用項目有很大的參考意見,感興趣的朋友可以移步上一篇,”Android Studion自定義Ed
本文主要講解局部加權(線性)回歸。在講解局部加權線性回歸之前,先講解兩個概念:欠擬合、過擬合,由此引出局部加權線性回歸算法。 欠擬合、過擬合如下圖中三個擬合模型
一、問題描述 在開發中,當我們需要的有一張大圖片同時還需要一些小圖片時,我們只需要通過代碼對此圖片進行不同比例的縮放即可,這樣大大節約資源,減小了安裝包的尺寸 。除縮