android–Sqlite数据库只应创建一次

android–Sqlite数据库只应创建一次,第1张

概述我开发了一个具有C2DM功能的项目.只要服务器上有数据,服务器将数据推送到该应用程序的所有设备(在我的情况下是一个小的消息).现在我的问题是每次创建消息来源的数据库和表时都会这样做这是我的代码,publicclassMessageHelperextendsActivity{publicString

我开发了一个具有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);        }    }

我还有一个名为MessageReceiver的类,它扩展了broadcastreceiver; eveytime一个消息来自类,它被执行,其中inturn加载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);    }}

我要找的是,第一次安装应用程序时应该创建一个数据库.

提前致谢,

TheIlleterate

解决方法:

数据库将仅创建一次,因为其名称未被更改.是的,它只会在您的应用首次启动MessageHelper活动时创建.

总结

以上是内存溢出为你收集整理的android – Sqlite数据库只应创建一次全部内容,希望文章能够帮你解决android – Sqlite数据库只应创建一次所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1117691.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存