2013-02-09 49 views
0

我剛開始嘗試在Android上。我正在閱讀Wrox開發的Android 4應用程序開發。有一個顯示通知的代碼。問題是我寫的代碼(未複製)很少修改。所以,我可以在狀態/通知欄上顯示通知,但是在點擊通知時,Notificaion類沒有被調用。這是首先android_mainfest的代碼。通知類不加載

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="legacy_systems.notificationproject" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-sdk 
    android:minSdkVersion="7" 
    android:targetSdkVersion="9" /> 
<uses-permission android:name="android.permission.VIBRATE"/> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name="legacy_systems.notificationproject.MainActivity" 
     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="Notification" 
     android:label="Details of the Notification" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.DEFAULT" />      
     </intent-filter> 
    </activity> 
</application> 

</manifest> 

現在,對於MainActivity 包legacy_systems.notificationproject代碼;

import android.os.Bundle; 
import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.content.Intent; 
import android.view.View; 
import android.app.Activity; 
import android.view.Menu; 

public class MainActivity extends Activity { 
int notificationID = 1; 

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

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.activity_main, menu); 
    return true; 
} 

public void onClick(View V) 
{ 
    getNotification(); 
} 

protected void getNotification() 
{ 
    Intent i = new Intent(this, Notification.class); 
    i.putExtra("notificationID", notificationID); 
    PendingIntent pn = PendingIntent.getActivity(this, 0, i, 0); 
    NotificationManager nm= (NotificationManager)getSystemService(NOTIFICATION_SERVICE); 
    Notification n = new Notification(R.drawable.ic_launcher, 
             "Reminder! Meeting starts in 5 Minute", 
             System.currentTimeMillis()); 
    CharSequence from = "System Alarm"; 
    CharSequence message = "Meeting with customer in next 2 minute"; 
    n.setLatestEventInfo(this, from, message, pn); 
    n.vibrate = new long[]{ 100,250,50,500}; 
    nm.notify(notificationID, n);  
} 

} 

而且,對於Notification.java代碼是 包legacy_systems.notificationproject;

import android.app.Activity; 
import android.os.Bundle; 
import android.app.NotificationManager; 
import android.util.Log; 

public class Notification extends Activity{ 
String tag; 

public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    Log.d(tag,"In here"); 

    setContentView(R.layout.notification); 

    NotificationManager nm = (NotificationManager)getSystemService(VIBRATOR_SERVICE); 
    nm.cancel(getIntent().getExtras().getInt("notificationID")); 
} 
} 

而且activity_main.xml中是,

<xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context=".MainActivity" > 

<Button 
    android:id="@+id/btn" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" 
    android:text="@string/getnot" 
    android:onClick="onClick" 
/> 

</RelativeLayout>  
+0

如何在清單中寫入android:name =「legacy_systems.notificationproject.Notification」'而不是'Notification'?另外,你的應用程序崩潰了嗎?如果有,請發佈堆棧跟蹤。 – 2013-02-09 19:41:44

+0

沒有。應用程序不會崩潰。什麼都沒發生。我點擊通知,它停留在那裏。至於名稱改變,它不起作用,我做了我的家務。 – Paras 2013-02-09 20:04:08

+1

我建議您切換到'Notification.Builder'(或Android Support Library的'NotificationCompat.Builder')。下面是一個示例項目,演示如何使用'Builder'顯示'Notification':https://github.com/commonsguy/cw-omnibus/tree/master/Notifications/DownloadNotify – CommonsWare 2013-02-09 20:05:53

回答

0

你會明智的不命名自己的類相同的名稱,系統定義的類。您試圖打開android.app.Notification作爲一項活動,該活動不起作用,並且應該導致LogCat中發出警告。

請您Notification活動重命名爲別的東西,比如Easdluaerdf,以使其獨特,調整你的清單,並PendingIntent匹配,你應該有更好的運氣。

+0

我也一樣。我重新命名了它。而且,通知仍然存在。點擊通知時,沒有任何反應。 – Paras 2013-02-10 06:22:14