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

手機應用能夠做爲桌面應用的輔助工具,用來接收桌面應用的狀態信息。這裏介紹如何實現一個簡單的Android程序用於接收Windows掃描儀應用的工做狀態。html

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

思路

  1. 建立socket鏈接用於應用通訊
    android

  2. Android上啓動後臺服務,用於接收信息git

  3. 在收到信息以後,後臺服務會把推送消息發送給Android應用
    github

Socket信息發送

使用TCPListener來建立socket鏈接,相關內容能夠參考:Wireless TWAIN Document Scanning on Androidwindows

啓動中止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:app

<
service android:name = "com.dynamsoft.twain.NotificationService" 
/>

onCreate(Bundle)中啓動服務:less

startService(new Intent(ScanAssist.this, NotificationService.class));

onDestroy()中中止服務:socket

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
相關文章
相關標籤/搜索