標籤:des android style http color io os 使用 ar
手機應用可以作為案頭應用的協助工具輔助,用來接收案頭應用的狀態資訊。這裡介紹如何?一個簡單的Android程式用於接收Windows掃描器應用的工作狀態。
參考:How to Push Notifications to Android Applications from Windows
思路
建立socket串連用於應用通訊
Android上啟動後台服務,用於接收資訊
在收到資訊之後,後台服務會把推送訊息發送給Android應用
Socket資訊發送
使用TCPListener來建立socket串連,相關內容可以參考:Wireless TWAIN Document Scanning on Android
啟動停止Android Service
建立服務NotificationService:
public class NotificationService extends Service { @Override public void onCreate() { } @Override public void onDestroy() { } @Override public IBinder onBind(Intent intent) { return mBinder; } private final IBinder mBinder = new Binder() { @Override protected boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws RemoteException { return super.onTransact(code, data, reply, flags); } };}
在AndroidManifest.xml中申明一下這個service:
<service android:name = "com.dynamsoft.twain.NotificationService" />
在onCreate(Bundle)中啟動服務:
startService(new Intent(ScanAssist.this, NotificationService.class));
在onDestroy()中停止服務:
stopService(new Intent(ScanAssist.this, NotificationService.class));
推送Android通知
調用NotificationManager:
private NotificationManager mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
建立用於內容顯示的Activity IncomingMessageView:
public class IncomingMessageView extends Activity { public static final String KEY_FROM = "from"; public static final String KEY_MESSAGE = "message"; public static final int NOTIFICATION_ID = R.layout.activity_main; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView view = new TextView(this); view.setText(getIntent().getCharSequenceExtra(KEY_FROM) + ": " + getIntent().getCharSequenceExtra(KEY_MESSAGE)); setContentView(view); NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); nm.cancel(NOTIFICATION_ID); }}
在AndroidManifest.xml中申明Activity:
<activity android:name="com.dynamsoft.twain.IncomingMessageView" android:label="@string/app_name" ></activity>
發送訊息,並顯示在狀態列上:
Intent notifyIntent = new Intent(this, IncomingMessageView.class);notifyIntent.putExtra(IncomingMessageView.KEY_FROM, from);notifyIntent.putExtra(IncomingMessageView.KEY_MESSAGE, message); PendingIntent pendingIntent = PendingIntent.getActivity( this, 0, notifyIntent, PendingIntent.FLAG_ONE_SHOT); Notification notif = new Notification.Builder(this).setContentTitle("TWAIN Scanner Status ").setContentText(message).setSmallIcon(R.drawable.ic_launcher).setContentIntent(pendingIntent).setTicker(message).build(); notif.defaults = Notification.DEFAULT_ALL; mNM.notify(IncomingMessageView.NOTIFICATION_ID, notif); 用例
運行Android應用,啟動服務
應用程式切換到後台,操作其它應用,比如開啟瀏覽器
在Windows上操作應用,點擊檔案掃描
掃描成功之後,資訊發送到手機
源碼
https://github.com/DynamsoftRD/ScanAssist
git clone https://github.com/DynamsoftRD/ScanAssist.git
如何從Windows應用發送通知訊息給Android應用