我想在onPostExecute方法之后执行通知。 我将android-support-v4.jar添加到libs文件夹,然后添加Build Path。 ConnectivityChangeReceiver类用于意识到设备何时连接到Internet。 我的解析没问题。 但是我得到了这些错误:
03-13 23:38:57.366: E/TAG(24253): TAGTTTTTTTTTTTT
03-13 23:38:59.231: E/TAG(24253): TAG
03-13 23:38:59.236: E/AndroidRuntime(24253): FATAL EXCEPTION: main
03-13 23:38:59.236: E/AndroidRuntime(24253): java.lang.NullPointerException
03-13 23:38:59.236: E/AndroidRuntime(24253): at com.example.ex80.ConnectivityChangeReceiver$CheckUpdate.onPostExecute(ConnectivityChangeReceiver.java:158)
03-13 23:38:59.236: E/AndroidRuntime(24253): at com.example.ex80.ConnectivityChangeReceiver$CheckUpdate.onPostExecute(ConnectivityChangeReceiver.java:1)
03-13 23:38:59.236: E/AndroidRuntime(24253): at android.os.AsyncTask.finish(AsyncTask.java:631)
03-13 23:38:59.236: E/AndroidRuntime(24253): at android.os.AsyncTask.access$600(AsyncTask.java:177)
03-13 23:38:59.236: E/AndroidRuntime(24253): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
03-13 23:38:59.236: E/AndroidRuntime(24253): at android.os.Handler.dispatchMessage(Handler.java:99)
03-13 23:38:59.236: E/AndroidRuntime(24253): at android.os.Looper.loop(Looper.java:137)
03-13 23:38:59.236: E/AndroidRuntime(24253): at android.app.ActivityThread.main(ActivityThread.java:5328)
03-13 23:38:59.236: E/AndroidRuntime(24253): at java.lang.reflect.Method.invokeNative(Native Method)
03-13 23:38:59.236: E/AndroidRuntime(24253): at java.lang.reflect.Method.invoke(Method.java:511)
03-13 23:38:59.236: E/AndroidRuntime(24253): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
03-13 23:38:59.236: E/AndroidRuntime(24253): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
03-13 23:38:59.236: E/AndroidRuntime(24253): at dalvik.system.NativeStart.main(Native Method)
我的BroadcastReceiver:
public class ConnectivityChangeReceiver extends BroadcastReceiver {
int version ;
boolean shoudupdate = false ;
boolean isconnectd = false ;
NotificationManager notificationManager = null ;
Notification notification = null ;
@SuppressLint("NewApi")
@Override
public void onReceive(Context context, Intent intent) {
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Intent myintent = new Intent(context, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, myintent, 0);
Notification.Builder builder = new Notification.Builder(context);
builder.setContentIntent(pIntent)
.setSmallIcon(R.drawable.ic_launcher)
.setTicker("notificationMessage")
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setSound(soundUri)
.setContentTitle("newNotificationsCount" + " New Notifications")
.setContentText("notificationMessage");
notification = builder.getNotification();
//
PackageInfo pInfo = null;
try {
pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
version = pInfo.versionCode;
isconnectd = isConnected(context);
Log.e("TAG", "TAGTTTTTTTTTTTT");
new CheckUpdate().execute((Void)null);
}
public boolean isConnected(Context context) {
ConnectivityManager connectivityManager = ((ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE));
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
return networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected();
}
private void shoudUpdate(){
URL url ;
try{
String feed = "*********************.xml" ;
url = new URL(feed);
URLConnection connection ;
connection = url.openConnection();
HttpURLConnection httpconnection = (HttpURLConnection)connection;
int responsecode = httpconnection.getResponseCode();
if(responsecode == HttpURLConnection.HTTP_OK){
InputStream in = httpconnection.getInputStream();
DocumentBuilderFactory dbf =DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder() ;
Document dom = db.parse(in);
Element docelm = dom.getDocumentElement();
NodeList nl = docelm.getElementsByTagName("item");
if(nl != null && nl.getLength() > 0 ){
//get version
Element entry = (Element)nl.item(0);
Element vs = (Element)entry.getElementsByTagName("version").item(0);
String updateversion = vs.getFirstChild().getNodeValue();
int currentversion = version ;
if(currentversion < Integer.parseInt(updateversion)){
shoudupdate = true ;
}else
shoudupdate = false ;
}
}
} catch (SAXException e) {
// TODO: handle exception
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private class CheckUpdate extends AsyncTask<Void, Void, Void>{
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
shoudUpdate();
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
if(isconnectd==true && shoudupdate==true){
Log.e("TAG", "TAG");
notificationManager.notify(0, notification);
}
super.onPostExecute(result);
}
}
}
我的宣言:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ex80"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.ex80.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>
<receiver android:name=".ConnectivityChangeReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
</application>
</manifest>
答案 0 :(得分:1)
您没有初始化notificationManager属性:
NotificationManager notificationManager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
请添加以上行并再次检查。
答案 1 :(得分:0)
您的通知管理器设置为null。初始化它。 Read this