スマートウォッチに通知を投稿するアプリを作成しようとしています。素晴らしい通知サンプル アプリケーションを見つけました。これをテスト用に変更しましたが、期待どおりに動作します。表示する必要がある拡張情報は、RemoteViews bigContentView にあることがわかりました。探しているビューを膨らませることができる Botification というプログラムを見つけました。彼のアプリケーションでは期待どおりに機能します。私は彼の方法を私の方法と結婚させようとしていますが、うまくいきません。また、Notify Me! のソースも見つけました。これは、通知リモート ビューをプルするために同じタイプのメソッドを使用しています。
これは私の最初の本物の Android アプリであり、Java は初めてです。私はこれに約 1 か月間取り組んでおり、このサイトの投稿や Web の閲覧から TON を学びました。Java Essentials と Android Essentials の Lynda.com コースを受講しました。また、これはJavaが初めてで、C++のバックグラウンドがほとんどない私にとって便利であることがわかりました:http://chortle.ccsu.edu/java5/index.html。
私が使用したいくつかの情報源: Parcelable、contentView、または contentIntent から通知テキストを抽出し、http: //www.kpbird.com/2013/07/android-notificationlistenerservice.html
どんな助けでも大歓迎です。Botification アプリケーションは onPosted リクエストを処理するためにハンドラーと別の方法を使用します。私の脳を太らせるためのドキュメントやチュートリアルは大歓迎です。
このリモート ビュー機能を使用するコードのセクションを含めていますが、うまくいけば、それがどのように機能するかについて理解できる範囲で十分です。
MainActivity.java public class MainActivity extends Activity {
private TextView txtView;
private NotificationReceiver nReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtView = (TextView) findViewById(R.id.textView);
nReceiver = new NotificationReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction("com.example.snotify.NOTIFICATION_LISTENER");
registerReceiver(nReceiver,filter);
}
nlservice.java public class NLService extends NotificationListenerService {
private String TAG = "NLService";
private NLServiceReceiver nlreceiver;
@Override
public void onCreate() {
super.onCreate();
nlreceiver = new NLServiceReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction("com.example.snotify.NOTIFICATION_LISTENER_SERVICE");
registerReceiver(nlreceiver,filter);
}
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(nlreceiver);
}
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
Notification n = sbn.getNotification();
Log.i(TAG,"!!!onNotificationPosted");
Log.i(TAG,"ID :" + sbn.getId() + "Ticker:" + n.tickerText + "Pkg:" + sbn.getPackageName());
String text = NotiCrunch.extractTextFromNotification(NLService.this, n);
String description = "";
if (n.tickerText != null) {
description = n.tickerText.toString();
}
Intent i = new Intent("com.example.snotify.NOTIFICATION_LISTENER");
i.putExtra("notification_event","Notification Posted\nPackage:" + sbn.getPackageName() + "\nID:" + sbn.getId()
+ "\nText:" + text
+ "\nTag:" + sbn.getTag() + "\nTicker:" + description + "\n\n");
sendBroadcast(i);
}
noticrunch.java
public class NotiCrunch {
private static String TAG = "NotiCrunch";
private static final int TIMESTAMPID = 16908388;
private static void extractViewType(ArrayList<View> outViews, Class<TextView> viewtype, View source) {
if (ViewGroup.class.isInstance(source)) {
ViewGroup vg = (ViewGroup) source;
for (int i = 0; i < vg.getChildCount(); i++) {
extractViewType(outViews, viewtype, vg.getChildAt(i));
}
} else if(viewtype.isInstance(source)) {
outViews.add(source);
}
}
public static String extractTextFromNotification(Service service, Notification notification) {
ArrayList<String> result = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
result = extractTextFromNotification(service, notification.bigContentView);
Log.d(TAG, "It tried Big View");
Log.d(TAG, "Ticker:" + notification.tickerText);
}
if (result == null) {
result = extractTextFromNotification(service, notification.contentView);
Log.d(TAG, "It tried little view");
}
if (result == null){
Log.d(TAG, "It is returning null");
return "";
}
return TextUtils.join("\n", result);
}
private static ArrayList<String> extractTextFromNotification(Service service, RemoteViews view) {
LayoutInflater inflater = (LayoutInflater) service.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ArrayList<String> result = new ArrayList<String>();
if (view == null) {
Log.d(TAG, "Initial view is Empty");
return null;
}
try {
ViewGroup localView = (ViewGroup) inflater.inflate(view.getLayoutId(), null);
view.reapply(service.getApplicationContext(), localView);
ArrayList<View> outViews = new ArrayList<View>();
extractViewType(outViews, TextView.class, localView);
for (View ttv: outViews) {
TextView tv = (TextView) ttv;
String txt = tv.getText().toString();
if (!TextUtils.isEmpty(txt) && tv.getId() != TIMESTAMPID) {
result.add(txt);
}
}
} catch (Exception e) {
Log.d(TAG, "FAILED to load notification! " + e.toString());
Log.wtf(TAG, e);
return null;
}
Log.d(TAG, "Return result" + result);
return result;
}
ありがとうございました。