Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在后台服务中读取收件箱邮件时出错_Android_Service - Fatal编程技术网

Android 在后台服务中读取收件箱邮件时出错

Android 在后台服务中读取收件箱邮件时出错,android,service,Android,Service,我试图在后台服务中读取收件箱中的未读短信,它正在读取短信,但在运行时读取短信后会强制关闭 public class MyService extends Service { private static final String TAG = "MyService"; MediaPlayer player; Context context; String value=""; int flag; int unreadMessagesCount; @Override public void onCrea

我试图在后台服务中读取收件箱中的未读短信,它正在读取短信,但在运行时读取短信后会强制关闭

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>

更改这行代码-

 Cursor c = getContentResolver().query(SMS_INBOX, null, "read = 0", null, null); 


你的MyService里有什么?它给出了NPE。用代码编辑您的问题。您尚未在类中实例化对象。请发布您的java代码,以便我们可以帮助您找到确切的错误。很抱歉,我再次收到错误04-24 14:04:21.992:E/AndroidRuntime(19097):致命异常:main 04-24 14:04:21.992:E/AndroidRuntime(19097):java.lang.RuntimeException:无法创建服务com.example.MyService:java.lang.NullPointerException 04-24 14:04:21.992:E/AndroidRuntime(19097):位于android.app.ActivityThread.handleCreateService(ActivityThread.java:1959)您不能使用此服务来启动意图您必须通过实现扩展应用程序的类来获取appcontext。公共类SMSApplication extensed application{private static Context;@Override public void onCreate(){super.onCreate();SMSApplication.Context=getApplicationContext();}公共静态上下文getAppContext(){return SMSApplication.context;}}现在在您的服务类中使用这个类getappcontext mtd。context context=SMSApplication.getappcontext();使用这个context对象并删除您在服务中使用的“this”。最幸运的是收到这个错误E/AndroidRuntime(25118):致命异常:main E/AndroidRuntime(25118):java.lang.RuntimeException:无法创建服务com.example.MyService:java.lang.NullPointerException E/AndroidRuntime(25118):位于android.app.ActivityThread.handleCreateService(ActivityThread.java:1959)E/AndroidRuntime(25118):致命异常:main04-24 E/AndroidRuntime(25118):java.lang.RuntimeException:无法创建服务com.example.MyService:java.lang.NullPointerException E/AndroidRuntime(25118):atandroid.app.ActivityThread.handleCreateService(ActivityThread.java:1959)
Cursor cursor = context.getContentResolver()
                .query(SMS_INBOX, null,"read = 0", null, null);