2012-01-03 8 views
5

プログレスバーと削除ボタンを使用してカスタム通知を作成したいとします。Googleの音楽のようなAndroid 4.0のカスタム通知

スクリーンショット:

enter image description here

私は、プログレスバーとカスタム通知を作成するには成功したが、私は削除イベントを作成するために管理していませんでした。ユーザーが「x」をクリックすると通知をキャンセルしてアップロードを停止したい(googleの音楽のように、通知をクリアするアクティビティを開始したくない)。

ここ

が私の作業コードです:行

Intent notificationIntent = new Intent(context, UploadEventsReceiver.class); 

notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE); 

    int icon = R.drawable.ic_launcher; 
    CharSequence tickerText = "Hello"; 
    long when = System.currentTimeMillis(); 

    notification = new Notification(icon, tickerText, when);  


    CharSequence contentTitle = "My notification"; 
    CharSequence contentText = "Hello World!"; 
    Intent notificationIntent = new Intent(context, UploadEventsReceiver.class); 
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); 

    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); 
    notification.contentView = new RemoteViews(context.getPackageName(), R.layout.upload_progress); 
    notification.contentView.setOnClickPendingIntent(R.id.text, contentIntent); 

    notificationManager.notify(this.notificationId, notification); 

私は「放送」することなく、放送受信機と試みたが、それを行うための正しい方法かどうかはわかりませんそれと私はそれを機能させるために成功しなかった。何を使用すればいいのですか?

+0

ニース、私はちょうどあなたのスクリーンショットで見つけたものを実装しようとしていましたWiFi、ローテーション、またはフライトモードの場合。あなたはカスタムROMを実行していますか、それともマーケットで見つけましたか?プロジェクトにご協力いただきありがとうございます。 – stfn

答えて

5

私はそれがgoogleの音楽でどのように作られているのかわかりませんが、RemoteViewsに渡されたレイアウトのどのビューでもonClickListenerを設定できます。ただ、このように実行します。

RemoteViews remoteViews = new RemoteViews(widgetPackage, R.layout.widget); 
Intent action = new Intent(context, Your_cancel_broadcast.class); 
action.setAction(CANCEL_BROADCAST_ACTION); 
actionPendingIntent = PendingIntent.getBroadcast(context, 0, action, 0); 
remoteViews.setOnClickPendingIntent(R.id.your_x_btn, actionPendingIntent); 

CANCEL_BROADCAST_ACTIONを受け取り、あなたが望むものを停止し、この通知をキャンセルする放送を、作成します。

+0

インテントはブロードキャストされますか?または、その予定は私のcancel_broadcastにのみ送信されますか? – Quanturium

+0

これは 'CANCEL_BROADCAST_ACTION'の値に依存します - カスタム(" lalala "のようなsmth)の場合、あなたのブロードキャストだけがこのインテントを受け取ります – Jin35

0

また、(ドキュメントによると)あなたのメソッドは廃止予定だと思います。

あなたがかなったように使用すべきである:例えば、きちんとしたハードウェア制御トグル:

Notification.Builder builder = new Notification.Builder(context); 
builder.setContentTitle("Your notification title"); 
builder.setContentText("Your notification text"); 
builder.setSmallIcon(R.drawable.ic_your_icon); 
builder.setContentIntent(yourIntent); 
notificationManager.notify(null, YOUR_UNIQUE_NOTIFICATION_ID, builder.getNotification()); 

http://developer.android.com/reference/android/app/NotificationManager.html

http://developer.android.com/reference/android/app/Notification.Builder.html

+1

注:Notification.BuilderはAPI 11+でのみ利用可能です。 v4サポートライブラリにはNotificationBuilderCompatがありますが、次のリンクによれば現在setProgressメソッドがありません。 http://code.google.com/p/android/issues/detail?id=30755 i – MaximumGoat

関連する問題