如何從Windows應用發送通知訊息給Android應用

來源:互聯網
上載者:User

標籤:des   android   style   http   color   io   os   使用   ar   

手機應用可以作為案頭應用的協助工具輔助,用來接收案頭應用的狀態資訊。這裡介紹如何?一個簡單的Android程式用於接收Windows掃描器應用的工作狀態。

參考:How to Push Notifications to Android Applications from Windows

思路
  1. 建立socket串連用於應用通訊

  2. Android上啟動後台服務,用於接收資訊

  3. 在收到資訊之後,後台服務會把推送訊息發送給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);
  用例
  1. 運行Android應用,啟動服務

  2. 應用程式切換到後台,操作其它應用,比如開啟瀏覽器

  3. 在Windows上操作應用,點擊檔案掃描

  4. 掃描成功之後,資訊發送到手機

源碼

https://github.com/DynamsoftRD/ScanAssist

git clone https://github.com/DynamsoftRD/ScanAssist.git


如何從Windows應用發送通知訊息給Android應用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.