我正在尝试在后台服务的收件箱中读取未读短信,它正在读取短信但在运行时读取短信后获取forceClose。
public class MyService extends Service {
private static final String TAG = "MyService";
MediaPlayer player;
Context context;
String value="";
int flag;
int unreadMessagesCount;
@Override
public void onCreate() {
Log.d(TAG, "onCreate");
player = MediaPlayer.create(this, R.raw.gossip_blast);
player.setVolume(50,50);
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
final Uri SMS_INBOX = Uri.parse("content://sms/inbox");
Cursor c = getContentResolver().query(SMS_INBOX, null, "read = 0", null, null);
unreadMessagesCount = c.getCount();
String cnt=Integer.toString(unreadMessagesCount);
String val="";
while(c.moveToNext())
{
value= c.getString(c.getColumnIndex("body"));
val=val+ c.getString(c.getColumnIndex("body"))+"\n";
if(value.indexOf("help")>0)
{
Toast.makeText(this,"HELP", Toast.LENGTH_LONG).show();
player.start();
// Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
//vibrator.vibrate(2000);
String notificationService = Context.NOTIFICATION_SERVICE;
NotificationManager notificationManager = (NotificationManager) getSystemService(notificationService);
Notification notification = new Notification(R.drawable.images,value, System.currentTimeMillis());
Intent notificationIntent = new Intent(getApplicationContext(), DaySixteenActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0,notificationIntent, 0);
notification.setLatestEventInfo(getApplicationContext(), value, value,contentIntent);
notificationManager.notify(1, notification);
}
}
c.deactivate();
c.close();
}
public void startAlert(View view) {
Intent intent = new Intent(this, read_sms.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 234324243, intent, 0);
Toast.makeText(this, "Alarm set in " ,
Toast.LENGTH_LONG).show();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
}
@Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
}
@Override
public IBinder onBind(Intent intent)
{
return null;
} }
收到此错误
04-23 11:36:55.719: E/AndroidRuntime(7509): FATAL EXCEPTION: main
04-23 11:36:55.719: E/AndroidRuntime(7509): java.lang.RuntimeException: Unable to instantiate service com.example.MyService: java.lang.NullPointerException
04-23 11:36:55.719: E/AndroidRuntime(7509): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2388)
04-23 11:36:55.719: E/AndroidRuntime(7509): at android.app.ActivityThread.access$1600(ActivityThread.java:140)
04-23 11:36:55.719: E/AndroidRuntime(7509): at dalvik.system.NativeStart.main(Native Method)
04-23 11:36:55.719: E/AndroidRuntime(7509): Caused by: java.lang.NullPointerException
04-23 11:36:55.719: E/AndroidRuntime(7509): at android.content.ContextWrapper.getContentResolver(ContextWrapper.java:91)
04-23 11:36:55.719: E/AndroidRuntime(7509): at java.lang.Class.newInstanceImpl(Native Method)
我在清单文件中包含了权限
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<service android:enabled="true" android:name=".MyService" />
<receiver android:name=".MyService" android:exported="true">
<intent-filter android:priority="999" >
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
答案 0 :(得分:0)
更改代码的这一行 -
Cursor c = getContentResolver().query(SMS_INBOX, null, "read = 0", null, null);
到
Cursor cursor = context.getContentResolver()
.query(SMS_INBOX, null,"read = 0", null, null);