2012-08-31 29 views
6

Tôi đã chỉnh sửa mã bằng cách sử dụng mục đích setOnClick đang chờ xử lý và mã này hoạt động cho ImageView và văn bản thông báo riêng biệt theo ý muốn nhưng tôi vẫn cần trợ giúp.Thông báo tùy chỉnh với chế độ xem từ xa trên Android

Tôi muốn tạm dừng hoặc phát mediaplayer từ lớp dịch vụ của mình, vậy làm cách nào tôi có thể truy cập vào phương thức dừng hoặc phát dịch vụ từ mục đích Thông báo đang chờ xử lý?

Một số người cho biết máy thu phát sóng sẽ giúp bạn, nhưng tôi không biết chính xác nó sẽ hoạt động như thế nào. Tôi đã quản lý để mở trình duyệt web từ nút tạm dừng từ thông báo, nhưng tôi không biết cách truy cập các phương thức dịch vụ. Chia sẻ bất kỳ mã mẫu nào nếu bạn có.

@SuppressWarnings("deprecation") 
void showNotification() { 
    int pendingRequestCode = 0; 
     int pendingFlag = 0; 

     final Resources res = getResources(); 
     final NotificationManager notificationManager = (NotificationManager) getSystemService(
       NOTIFICATION_SERVICE); 
     Intent intent = new Intent(this,MainActivity.class); 
     PendingIntent pi= PendingIntent.getActivity(this, 0, intent, 0); 
     Notification.Builder builder = new Notification.Builder(this) 
       .setSmallIcon(R.drawable.ic_action_search) 
       .setAutoCancel(true) 
       .setTicker("this is notification") 
       .setContentIntent(pi); 


     // Sets a custom content view for the notification, including an image button. 
     RemoteViews layout = new RemoteViews(getPackageName(), R.layout.notification); 
     layout.setTextViewText(R.id.notification_title, getString(R.string.app_name)); 
     Intent clickIntent = new Intent(Intent.ACTION_VIEW, Uri_myBlog); 
     PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(),pendingRequestCode, clickIntent, pendingFlag); 
     layout.setOnClickPendingIntent(R.id.notification_button,pendingIntent); 
     builder.setContent(layout); 

     // Notifications in Android 3.0 now have a standard mechanism for displaying large 
     // bitmaps such as contact avatars. Here, we load an example image and resize it to the 
     // appropriate size for large bitmaps in notifications. 
     Bitmap largeIconTemp = BitmapFactory.decodeResource(res, 
       R.drawable.pause); 
     Bitmap largeIcon = Bitmap.createScaledBitmap(
       largeIconTemp, 
       res.getDimensionPixelSize(android.R.dimen.notification_large_icon_width), 
       res.getDimensionPixelSize(android.R.dimen.notification_large_icon_height), 
       false); 
     largeIconTemp.recycle(); 

     builder.setLargeIcon(largeIcon); 

     notificationManager.notify(NOTIFICATION_DEFAULT, builder.getNotification()); 
    } 

    PendingIntent getDialogPendingIntent(String dialogText) { 
     return PendingIntent.getActivity(
       this, 
       dialogText.hashCode(), // Otherwise previous PendingIntents with the same 
             // requestCode may be overwritten. 
       new Intent(ACTION_DIALOG) 
         .putExtra(Intent.EXTRA_TEXT, dialogText) 
         .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK), 
       0); 
    } 
+0

Cung cấp cho tôi một gợi ý. tôi xây dựng một cái nhìn thông báo lớn sử dụng để kiểm soát RemoteView chơi/tạm dừng như liên kết này (http://stackoverflow.com/questions/14508369/how-to-create-a -notification-similar-to-play-music-app-from-google) Tất cả đều đúng nhưng khi tôi nhấp vào nút quay lại thiết bị và ra khỏi ứng dụng nút bấm sự kiện ion (Phát/Tạm dừng/Chuyển tiếp/Đóng) không hoạt động.Vui lòng giúp tôi. –

Trả lời

6

Bạn không biết bạn đang nhắm mục tiêu phiên bản Android nào, nhưng nói chung không thể cho đến Android 3.x (Tổ ong). Vì vậy, nếu bạn muốn điều đó xảy ra trên 2.x, thì xin lỗi - bạn đã hết may mắn.

Đối với tổ ong, bạn chỉ cần cung cấp bố cục tùy chỉnh và gán PendingIntent cho mỗi nút bạn cần, bằng cách gọi setOnClickPendingIntent(). Nếu bạn bị mẫu của SDK tải về, sau đó xem MainActivity của ứng dụng HoneycombGallery (phải trong thư mục <SDK>\samples\android-XX\HoneycombGallery\ của bạn. XX là bất cứ điều gì từ 14 (hoặc cao hơn).

+0

mục tiêu của tôi là min 2.2 đến 4.1 ... ok của nó mà froyo sẽ không hỗ trợ cơ sở này .. nhưng người dùng tổ ong và jellybean có thể sử dụng nó trong ứng dụng của tôi xin vui lòng hướng dẫn tôi cho các tính năng này –

+0

xem câu trả lời đã chỉnh sửa –

+0

là có cách nào để kiểm tra số API Android và theo đó tôi có thể thay đổi kiểu thông báo của tôi như bình thường cho ít hơn 2.2 và thông báo nút cho 2.3 trở lên –

Các vấn đề liên quan