Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/208.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
Java 将数据从parse插入sqllite数据库_Java_Android_Parse Platform_Android Database - Fatal编程技术网

Java 将数据从parse插入sqllite数据库

Java 将数据从parse插入sqllite数据库,java,android,parse-platform,android-database,Java,Android,Parse Platform,Android Database,我正在尝试获取通知内容并将其自动存储到数据库中,一旦parse.com发布通知,无论用户是否单击以查看通知,数据都应保存在数据库中 这是我迄今为止尝试过的,但仍然不起作用。它仅在用户单击通知时将内容保存到数据库中 Application.java public class Application extends android.app.Application{ public Application() { } @SuppressWarnings("deprecation")

我正在尝试获取通知内容并将其自动存储到数据库中,一旦parse.com发布通知,无论用户是否单击以查看通知,数据都应保存在数据库中

这是我迄今为止尝试过的,但仍然不起作用。它仅在用户单击通知时将内容保存到数据库中

Application.java

public class Application extends android.app.Application{
  public Application() {
  }

  @SuppressWarnings("deprecation")
    public void onCreate() {
    super.onCreate();
      // Initialize the Parse SDK.
    Parse.initialize(this, "YwWVJ1IdukAXQx6WqksoRlA94k1OoJ6cHqdgsInHaTN", "fCh5pWNiSaHaFtuACufgs9va6wq31pte8nuaiCAG6Nb");

    // Specify an Activity to handle all pushes by default.
    PushService.setDefaultPushCallback(this, Notification.class);
  }        
} 
public class Notification extends ListActivity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ParseAnalytics.trackAppOpened(getIntent());
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        String message="";

         SQLiteDatabase db;

            db = openOrCreateDatabase(
                "notification.db"
                , SQLiteDatabase.CREATE_IF_NECESSARY
                , null
                );

          //CREATE TABLES AND INSERT MESSAGES INTO THE TABLES
            String CREATE_TABLE_NOTICES = "CREATE TABLE IF NOT EXISTS notices ("
                    + "ID INTEGER primary key AUTOINCREMENT,"
                    + "NOTIFICATIONS TEXT)";
            db.execSQL(CREATE_TABLE_NOTICES);

        if(extras !=null){
            String jsonData = extras.getString("com.parse.Data");
            try{
                JSONObject notification = new JSONObject(jsonData);
                 message = notification.getString("alert");

                    String sql =
                        "INSERT or replace INTO notices (NOTIFICATIONS) "
                        + "VALUES('" + message + "')";       
                    db.execSQL(sql);      

            }
            catch(JSONException e){
                Toast.makeText(getApplicationContext(), "Something went wrong with the notification", Toast.LENGTH_SHORT).show();
            }   
        }      
}
FireBackground.java

public class FireBackground extends ParsePushBroadcastReceiver {
    @Override
    public void onPushOpen(Context context, Intent intent) {
        Intent i = new Intent(context, Notification.class);
        i.putExtras(intent.getExtras());
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
    }
}
Notification.java

public class Application extends android.app.Application{
  public Application() {
  }

  @SuppressWarnings("deprecation")
    public void onCreate() {
    super.onCreate();
      // Initialize the Parse SDK.
    Parse.initialize(this, "YwWVJ1IdukAXQx6WqksoRlA94k1OoJ6cHqdgsInHaTN", "fCh5pWNiSaHaFtuACufgs9va6wq31pte8nuaiCAG6Nb");

    // Specify an Activity to handle all pushes by default.
    PushService.setDefaultPushCallback(this, Notification.class);
  }        
} 
public class Notification extends ListActivity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ParseAnalytics.trackAppOpened(getIntent());
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        String message="";

         SQLiteDatabase db;

            db = openOrCreateDatabase(
                "notification.db"
                , SQLiteDatabase.CREATE_IF_NECESSARY
                , null
                );

          //CREATE TABLES AND INSERT MESSAGES INTO THE TABLES
            String CREATE_TABLE_NOTICES = "CREATE TABLE IF NOT EXISTS notices ("
                    + "ID INTEGER primary key AUTOINCREMENT,"
                    + "NOTIFICATIONS TEXT)";
            db.execSQL(CREATE_TABLE_NOTICES);

        if(extras !=null){
            String jsonData = extras.getString("com.parse.Data");
            try{
                JSONObject notification = new JSONObject(jsonData);
                 message = notification.getString("alert");

                    String sql =
                        "INSERT or replace INTO notices (NOTIFICATIONS) "
                        + "VALUES('" + message + "')";       
                    db.execSQL(sql);      

            }
            catch(JSONException e){
                Toast.makeText(getApplicationContext(), "Something went wrong with the notification", Toast.LENGTH_SHORT).show();
            }   
        }      
}
android清单文件

<application
 android:name="com.parse.starter.Application"
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppBaseTheme" >
    <activity
        android:name="com.parse.starter.Notification"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    <service android:name="com.parse.PushService" />

    <receiver android:name=".HomeActivity"
     android:exported="false" > 
        <intent-filter>
        <action android:name="com.parse.push.intent.RECEIVE" />
        <action android:name="com.parse.push.intent.DELETE" />
        <action android:name="com.parse.push.intent.OPEN" />
    </intent-filter>
    </receiver>

    <receiver android:name="com.parse.ParseBroadcastReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.USER_PRESENT" />
        </intent-filter>
    </receiver>
    <receiver android:name="com.parse.GcmBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND">
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

            <!--
              IMPORTANT: If you change the package name of this sample app,
              change "com.parse.starter" in the lines
              below to match the new package name.
            -->
            <category android:name="com.parse.starter" />
        </intent-filter>
    </receiver>


如果有人能指导我使用正确的方法,我将不胜感激。感谢您将通知数据保存到本地数据库的代码在活动中运行。当通知打开时,活动开始,因此名称为
onPushOpen
。因此,使用类似于
onpushreceived(…)

的方法将保存到数据库的代码移动到
FireBackground
,您应该在您的BroadcastReceiver中覆盖onPushReceive方法,然后获取必要的数据 出于故意。 下面是我检索jsonData的示例:

@Override
protected void onPushReceive(Context context, Intent intent) {
   Bundle extras = intent.getExtras();

   String jsonData = extras.getString("com.parse.Data");
   DBServiceFlow.save(intent);
   } catch (JSONException | ParseException e) {
       Log.e(TAG, "", e);
   }
}
然后,您可以使用后台线程实现自己的服务来保存数据。例如,您可以使用


PS:不要在主线程中使用DB。(如您在通知活动中所示)

“我正在尝试获取通知内容并将其自动存储到数据库中”,然后您说问题已经解决了“只有当用户单击通知时才将内容保存到数据库中”。什么是明确的问题?我想你没有让我明白。仅当用户从其手机界面单击通知图标时,上述代码才能将通知内容保存到数据库中。但是,如果用户访问应用程序时没有单击其文件夹中的通知图标,则内容将不会保存在本地数据库中。@deadfish请告诉我,我现在清楚我的问题了吗?您好,请告诉我,我真的不明白您做了什么。你能再解释一下吗。ThanksDBHelper-静态类BD层DAO。DBService—接收DB iteractions请求的意图服务。将在后台线程中处理请求。DBServiceFlow-静态类,它将帮助您编写意图和调用服务。数据应该通过DBService类保存吗?您将数据存储到数据库的何处?您可以在OnHandleContent中解析数据,然后将其存储在静态类的save方法中。因此,您将把您的方法提取到DAO(数据访问对象)。请您使用我上面的代码给我一个说明,以便我更好地理解。谢谢