4

私は C2DM 機能を持つプロジェクトを開発しました。サーバーで利用可能なデータがあるときはいつでも。サーバーは、このアプリを使用するすべてのデバイスにデータをプッシュします (私の場合は小さなメッセージです)。

ここで私の質問は、データベースとテーブルが作成されるたびにメッセージが来る場所です。これが私のコードです。

 public class MessageHelper extends Activity
    {
        public String str; 
        public int sts;
        public Date timeStamp;

        protected void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            Bundle extras = getIntent().getExtras();
            if (extras != null) {
                this.str = extras.getString("msgg");
                Log.d("C2DM", this.str);
            }

            // Create a ref of SQLite
            SQLiteDatabase db;

            //use tat ref to open or create a table 
            db = openOrCreateDatabase( "/data/data/de.vogella.android.c2dm.simpleclient/app_database/file__0/0000000000000001.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);

            try 
            {
                //initialsiging a query with all the table fields 
                final String CREATE_TABLE_CONTAIN = "CREATE TABLE IF NOT EXISTS tbl_Message4("
                        + "id INTEGER PRIMARY KEY AUTOINCREMENT,"
                        + "msg TEXT not null," 
                        + "msg_time INTEGER not null,"
                        + "msg_status INTEGER not null);";

                //execute the above query
                db.execSQL(CREATE_TABLE_CONTAIN);

                timeStamp = new Date();

                String sql =
                        "INSERT or replace INTO tbl_Message4 (msg,msg_time,msg_status) VALUES('"+this.str+"' ,'"+this.timeStamp+"' , 0)" ;       
                db.execSQL(sql);
        }
    }

また、Broadcastreceiver を拡張する MessageReceiver というクラスがあります。メッセージが来るたびにクラスが実行され、それが MessageHelper.class をロードします。ここでは MessageReceiver のコードです。

public class C2DMMessageReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        Log.w("C2DM", "Message Receiver called");
        if ("com.google.android.c2dm.intent.RECEIVE".equals(action)) {
            Log.w("C2DM", "Received message");
            final String payload = intent.getStringExtra("payload");
            Log.d("C2DM", "dmControl: payload = " + payload);
            // TODO Send this to my application server to get the real data
            // Lets make something visible to show that we received the message
            createNotification(context, payload);

        }
    }

    public void createNotification(Context context, String payload) {
        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(R.drawable.ic_launcher,
                "Message received", System.currentTimeMillis());
        // Hide the notification after its selected
        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        //Intent intent = new Intent(context, MessageReceivedActivity.class);
        //intent.putExtra("payload", payload);

        Intent intent = new Intent(context, MessageHelper.class);
        intent.putExtra("msgg", payload);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
        //PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
        //notification.setLatestEventInfo(context, "Message", "New message received", pendingIntent);
        //notificationManager.notify(0, notification);

    }

}

私が探しているのは、アプリが初めてインストールされたときにデータベースを作成する必要があるということです。

前もって感謝します、

文盲

4

3 に答える 3

2

名前が変更されていないため、データベースは 1 回だけ作成されます。はい、アプリがアクティビティMessageHelperを初めて起動するときにのみ作成されます。

于 2012-05-11T11:44:18.707 に答える
0

「CREATE TABLE IF NOT EXISTS」は、テーブルがまだ存在しない場合にのみテーブルを作成します。そのため、一度だけ作成されます。このテーブルを別の場所で削除しますか??

于 2012-05-11T11:57:11.300 に答える
0

データベースは、1 つの名前で 1 回だけ作成されます。

ここで、このコードを選択して簡単にすることもできます

http://www.vogella.com/articles/AndroidSQLite/article.html

于 2012-05-11T11:47:43.143 に答える