2012-04-25 10 views
1

Androidの通知サービスのデモの例については、次のリンクを参照してください:​​およびVogella Tutorial通知サービスを実装できません

どちらも機能していますが、部分的には両方のプロジェクトをそのままダウンロードして実行しました。両方に通知を開始するボタンがあります。オンボタン上部のステータスバーにボタンのクリック通知が表示されます。

この通知をクリックすると、メッセージは表示されず、新しいアクティビティに移動する意図もありません。

私は任意のヘルプは感謝、この概念に新たなんだ...

CODE

CreateNotificationの.class

public class CreateNotification extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
    } 

    public void notify(View view) { 
     NotificationManager nm= (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
     final int UNIQUE_ID = 123458; 
     Intent navigationIntent = new Intent(); 
     navigationIntent.setClass(CreateNotification.this, 
       NotificationReceiver.class); 

     PendingIntent pi = PendingIntent.getActivity(this, 0, navigationIntent, 
       0); 
     String body = "New Notification added!!!"; 
     String title = "Title"; 
     Notification n = new Notification(R.drawable.ic_launcher, body, 
       System.currentTimeMillis()); 
     n.number = 2; 
     n.setLatestEventInfo(this, title, body, pi); 
     n.defaults = Notification.DEFAULT_ALL; 
     nm.notify(UNIQUE_ID, n); 
    } 
} 

NotificationReceiver.class

public class NotificationReceiver extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.result); 
     Log.i("Receiver", "NotificationReceiver"); 
    } 
} 

main.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:onClick="notify" 
     android:text="Create Notification" > 
    </Button> 

</LinearLayout> 

result.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="This is the result activity opened from the notification" > 
    </TextView> 

</LinearLayout> 

のAndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.notificationmanager" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk android:minSdkVersion="10" /> 
    <uses-permission android:name="android.permission.VIBRATE" /> 
    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" > 
     <activity 
      android:name=".CreateNotification" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name=".NotificationReceiver" /> 
    </application> 

</manifest> 
+0

AndroidManifest.xmlファイルも添付してください。 – Blehi

+0

うーん...すべてがよさそうだ。 APIデモをダウンロードしてください。いくつかの通知の例があります。リンク:http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/index.html#Notification – Blehi

+0

デモでは通知のタイトルの表示についてのみ説明していますが、そのタイトルをクリックするとどうなりますか説明されていません:( – GAMA

答えて

2

行を変更してみてください:

PendingIntent pi = PendingIntent.getActivity(this, 0, navigationIntent, 0); 

PendingIntent pi = PendingIntent.getActivity(this, 0, navigationIntent, Intent.FLAG_ACTIVITY_NEW_TASK); 

のドキュメント:

注活動は、既存の活動の文脈の外で開始されることをので、あなたインテントでIntent.FLAG_ACTIVITY_NEW_TASK起動フラグを使用する必要があります。

私のコードの下に添付されています。私にとっては適切に働いています。

private void showNotification() { 
    Log.i(TAG, "showNotification called..."); 
    final Intent notificationIntent = new Intent(this, UpdateApplicationActivity.class); 
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    final PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 

    final Notification notification = new Notification(R.drawable.htc_notification, getString(R.string.UpdateCheck_update_available), System.currentTimeMillis()); 
    notification.defaults |= Notification.DEFAULT_ALL; 
    notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE | Notification.FLAG_SHOW_LIGHTS | Notification.FLAG_AUTO_CANCEL; 
    notification.setLatestEventInfo(this, getString(R.string.UpdateCheck_notification_title), getString(R.string.UpdateCheck_notification_message), contentIntent); 

    // Notifying the user about the new update. 
    if (notificationManager != null) { 
     notificationManager.notify(APP_UPDATE_NOTIFICATION, notification); 
    } 
} 
+0

私は答えに自分のコードを添付...試してみてください。 – Blehi

関連する問題