针对AndroidGCM:未在onCreate()方法上获取注册令牌和未在lk中注册是什么意思这两个问题,本篇文章进行了详细的解答,同时本文还将给你拓展Android-当android释放在onCre
针对Android GCM:未在onCreate()方法上获取注册令牌和未在lk中注册是什么意思这两个问题,本篇文章进行了详细的解答,同时本文还将给你拓展Android - 当 android 释放在 onCreate 中创建的数据的内存时会发生什么、Android onCreate( )方法详细介绍、Android onCreate()方法在设备旋转时调用两次. (ICS)、Android onResume和onCreate等相关知识,希望可以帮助到你。
本文目录一览:- Android GCM:未在onCreate()方法上获取注册令牌(未在lk中注册是什么意思)
- Android - 当 android 释放在 onCreate 中创建的数据的内存时会发生什么
- Android onCreate( )方法详细介绍
- Android onCreate()方法在设备旋转时调用两次. (ICS)
- Android onResume和onCreate
Android GCM:未在onCreate()方法上获取注册令牌(未在lk中注册是什么意思)
这是我想要打印GCM regID的onCreate()方法:
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final String regId = GCMRegistrar.getRegistrationId(this); GCM_regID = regId; System.out.println("GCM regId: "+GCM_regID);
在onCreate()中执行以下代码:
/** * Google Cloud Messaging - Getting server Url and device ID to send it * across Google server for the notification.. */ mGCMReceiver = new GCMReceiver(); mOnRegisteredFilter = new IntentFilter(); mOnRegisteredFilter.addAction(Constants.ACTION_ON_REGISTERED); if (Constants.SENDER_ID == null) { // mStatus.setText("Missing SENDER_ID"); return; } if (Constants.SERVER_URL == null) { // mStatus.setText("Missing SERVER_URL"); return; } GCMRegistrar.checkDevice(this); GCMRegistrar.checkManifest(this); if (!regId.equals("")) { sendIdToServer(regId); } else { GCMRegistrar.register(getApplicationContext(),Constants.SENDER_ID); } sendIdToServer(regId); }
按照以下教程之一的指导,通过这些方法将GCM_regId发送到服务器:
/** * GCM - sending the data in json format to server db * */ public void sendIdToServer(String regId) { (new SendRegistrationIdTask(regId)).execute(); GCM_regID = regId; } private class GCMReceiver extends broadcastReceiver { @Override public void onReceive(Context context,Intent intent) { String regId = intent .getStringExtra(Constants.FIELD_REGISTRATION_ID); token = regId; } } private final class SendRegistrationIdTask extends AsyncTask<String,Void,HttpResponse> { // private String mRegId; public SendRegistrationIdTask(String regId) { // mRegId = regId; } @Override protected HttpResponse doInBackground(String... regIds) { // String url = Constants.SERVER_URL + "/register"; return null; } @Override protected void onPostExecute(HttpResponse response) { if (response == null) { return; } StatusLine httpStatus = response.getStatusLine(); if (httpStatus.getStatusCode() != 200) { Log.e(Constants.TAG,"Status: " + httpStatus.getStatusCode()); return; } } }
我不这么认为,这里需要GCMIntentService类来解决我的问题.请仔细研究并帮助我解决这个问题.
我可以在onRegistered()上的GCMIntentService类中打印.在这里:
@Override protected void onRegistered(Context context,String regId) { Intent intent = new Intent(Constants.ACTION_ON_REGISTERED); intent.putExtra(Constants.FIELD_REGISTRATION_ID,regId); context.sendbroadcast(intent); }
我必须在onCreate()上的MainActivity上打印regId.
解决方法
protected void onRegistered(Context arg0,String arg1) { Logger.d(arg0,"REG ID="+arg1); regID = arg1; }
这是在Reg完成后调用的方法.所以对于我的情况,regID我把它当作一个静态字符串并且正在访问它.或者就像你想要活动B上的regID一样,最好在活动A上注册它,并通过GCMIntent类中的上述方法中的静态字符串检索它.
Android - 当 android 释放在 onCreate 中创建的数据的内存时会发生什么
如何解决Android - 当 android 释放在 onCreate 中创建的数据的内存时会发生什么?
假设我在 onCreate
中初始化了一堆东西,然后将应用程序带入后台一个小时。如果 Android 在我继续使用该应用程序时释放了在 onCreate
中初始化的内容,会发生什么情况?会再次调用 onCreate
吗?我之所以这么问是因为我考虑在 onResume
而不是 onCreate
中移动一些东西,如果再次调用 onCreate
,这是不必要的。
解决方法
如果 Android 释放在我继续使用应用程序时在 onCreate 内部初始化的内容会怎样?
一般来说,Android 不会这样做。 Android 可能终止您的进程,在这种情况下,一切都会消失,您将在下一次应用启动时从头开始。
但是,如果您的进程设法在后台存活了一个小时,那么大多数事情都会在您离开的地方保持原样。当您的应用移至后台时,某些库可能会专门尝试释放一些内存(例如,缓存在图像加载库中),以尝试降低您的进程被终止以释放系统 RAM 的可能性。
文档有 a bit of material on the process lifecycle。
onCreate 会被再次调用吗?
如果 Android 终止了您的进程,那么是的,将再次调用任何已启动活动的 onCreate()
。如果您的进程幸存下来,则 Activity 已经创建,因此不会再次调用 onCreate()
。 onStart()
和 onResume()
在任何一种情况下都会被调用。
文档有 some material on the activity lifecycle。
Android onCreate( )方法详细介绍
onCreate( )方法是android应用程序中最常见的方法之一,那么,我们在使用onCreate()方法的时候应该注意哪些问题呢?
先看看Google Android Developers官网上的解释:
onCreate(Bundle) is where you initialize your activity. Most importantly,here you will usually call setContentView(int) with a layout resource defining your UI,and using findViewById(int) to retrieve the widgets in that UI that you need to interact with programmatically.
Called when the activity is starting. This is where most initialization should go: calling setContentView(int) to inflate the activity‘s UI,using findViewById(int) to programmatically interact with widgets in the UI,calling managedQuery(android.net.Uri,String[],String,String) to retrieve cursors for data being displayed,etc.
You can call finish() from within this function,in which case onDestroy() will be immediately called without any of the rest of the activity lifecycle (onStart(),onResume(),onPause(),etc) executing.
Derived classes must call through to the super class‘s implementation of this method. If they do not,an exception will be thrown.
翻译过来就是说,onCreate()函数是在activity初始化的时候调用的,通常情况下,我们需要在onCreate()中调用setContentView(int)函数填充屏幕的UI,一般通过findViewById(int)返回xml中定义的视图或组件的ID。子类在重写onCreate()方法的时候必须调用父类的onCreate()方法,即super.onCreate(),否则会抛出异常。
但是,我们必须要注意的是,在onCreate()函数里我们需要配置一些必要的信息,但是并不是所有的事情都能在这里做。我们知道,一个activity启动调用的第一个函数就是onCreate,它主要做这个activity启动时一些必要的初始化工作,这个函数调用完后,这个activity并不是说就已经启动了,或者是跳到前台了。而是还需要其他的大量工作,我们知道:onCreate之后还有onRestart()和onStart()等,实际上onStart()调用完毕了这个activity还没有完全启动,也只是前台可见,直到 onResume() 调用后这个onCreate才算终于启动。既然这样,那么在一个activity真正启动之前任何相当耗时的动作都会导致activity启动缓慢,特别是在onCreate里面耗时长的话可能导致极差的用户体验。
下面来看一个例子:
protected void onCreate(Bundle savedInstanceState) { // Todo Auto-generated method stub super.onCreate(savedInstanceState); this.requestwindowFeature(Window.FEATURE_NO_TITLE); mContext = this; setContentView(R.layout.main); dataLoad = new DataLoading(); mScrollLayout = (ScrollLayout)findViewById(R.id.ScrollLayoutTest); btnExit = (ImageButton)findViewById(R.id.btn_exit); btnExit.setonClickListener(btnExitClickListener); btnContacts = (ImageButton)findViewById(R.id.btn_contacts); btnContacts.setonClickListener(btnContactsClickListener); mSpeedDailDataMgr = new SpeedDailMgr(this); loadgripView(); //in MTK //mCallOptionHandler = new CallOptionHandler(this); mCallOptionHandler = new ContactsCallOptionHandler(this,new ContactsCallOptionHandlerFactory()); //don't consider getting no data,ex: when starting up updateEnabledCard(); }
这是一个APP的一个Activity的onCreate的写法。其实这段代码没有什么问题,而且看起来也是比较简单的代码。不过里面大量危险的代码段:不管是dataLoad = new DataLoading(); 还是 mSpeedDailDataMgr = new SpeedDailMgr(this);更或者是loadgripView();甚至updateEnabledCard();这么危险的处理都是不应该在这里来处理的。这里包含了加载数据库数据、读取文件信息、读取SIM卡信息,这些操作都是有可能抛出异常的,而且其操作耗时也是不确定的!对于面对这样问题,我觉得应该注意下面几个方面:
(1)在Activity启动前,尽量少做。
(2)对于布局比较复杂的时候,可以考虑不要一次性全部加载上,动态加载是一个好的办法。
(3)对于及时需要的数据,加载起来耗时的又有异常危险的,一定记得开辟一个线程来做这些动作,千万记得不要做阻塞主线程(UI线程)的任何事情。
(4)对于特殊情况下,Activity启动确实需要大量工作时候,可以考虑先加载一个简单的布局(或是Activity)来过渡.。
(5)所有的目的都是让你要启动的组件尽快上场,而不是以画好妆为主,这样的话客人会等不及的,顾客就是上帝。
以上就是对Android OnCreate()方法的详细介绍,后续继续补充相关知识,谢谢大家对本站的支持!
Android onCreate()方法在设备旋转时调用两次. (ICS)
我通过在活动标记的Manifest.xml文件中设置此行来修复Honeycomb(3.1)上的这个问题,它运行良好.
android:configChanges="keyboardHidden|orientation"
但是当我在ICS Tablet上测试我的应用程序时,这个问题又回来了.有谁知道如何解决这个问题?
谢谢
问候.
解决方法
android:configChanges="keyboardHidden|orientation|screenSize"
Android onResume和onCreate
我对活动生命周期的逻辑有一个疑问:
当我学习Android时,总是在onCreate()函数中设置一个Activity.现在当我恢复我的Activity时,可能会在onResume()中完成已经在onCreate()中完成的事情.但是,为什么我们不只是把所有的东西放到onResume()?
解决方法:
why would we not just put all the stuff into onResume()?
在创建Activity时调用onCreate(),你需要初始化应用程序的一些非常重要的东西,比如你的主要布局!
今天关于Android GCM:未在onCreate()方法上获取注册令牌和未在lk中注册是什么意思的讲解已经结束,谢谢您的阅读,如果想了解更多关于Android - 当 android 释放在 onCreate 中创建的数据的内存时会发生什么、Android onCreate( )方法详细介绍、Android onCreate()方法在设备旋转时调用两次. (ICS)、Android onResume和onCreate的相关知识,请在本站搜索。
本文标签: