有没有可靠的方法来获得Context
一个Service
?
我想注册广播接收器,ACTION_PHONE_STATE_CHANGED
但我不需要我的应用程序总是得到这些信息,所以我不把它放在Manifest
.
但是,当我需要这些信息时,我不能让GC杀死广播接收器,因此我正在注册广播接收器Service
.
因此,我需要一个Context
来打电话registerReceiver()
.当我不再需要时,ACTION_PHONE_STATE_CHANGED
我取消注册它.
有小费吗?
我正在使用推荐的Up Navigation方法,我的代码如下所示:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent h = new Intent(ShowDetailsActivity.this, MainActivity.class);
h.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(h);
return true;
default: return super.onOptionsItemSelected(item);
}
}
Run Code Online (Sandbox Code Playgroud)
这是用例:
问题是在我点击UP之后,MainActivity再次点击其onCreate()方法并失去所有状态而不是从典型的onResume()开始,就像我刚刚从ShowDetailsActivity调用"finish()"一样.为什么?它是如何工作的,这是Android重新创建使用"向上"导航方法导航到的所有活动的预期行为?如果我点击后退按钮,我会得到预期的onResume生命周期.
如果Android"正确"的不存在,这是我的解决方案:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent upIntent = new Intent(this, MainActivity.class);
if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
NavUtils.navigateUpTo(this, upIntent);
finish();
} else {
finish();
}
return true;
default: return super.onOptionsItemSelected(item);
}
}
Run Code Online (Sandbox Code Playgroud) Android的ScrollView(当滚动或弹出时)喜欢设置EditText的焦点,当它是它的一个孩子时.滚动然后释放时会发生这种情况.反正有没有阻止这种行为?我已经尝试了几乎所有我能想到的东西以及我在StackOverflow上读过的所有内容.没有什么对我有用.下面是一个示例布局如果您想测试它.我迫切希望阻止这种愚蠢的行为.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ScrollView
android:id="@+id/scrollView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginRight="50dp"
android:focusable="true"
android:focusableInTouchMode="true"
>
<EditText
android:layout_width="fill_parent"
android:layout_height="100dp"
android:text="TestApp 1"
/>
<Button
android:id="@+id/btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TestApp 1"
/>
<Button
android:id="@+id/btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TestApp 1"
/>
<Button
android:id="@+id/btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TestApp 1"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="100dp"
android:text="Bleh...."
/>
<Button
android:id="@+id/btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TestApp 1"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="100dp"
android:text="TestApp 1"
/>
<Button
android:id="@+id/btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TestApp 1"
/>
<Button
android:id="@+id/btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="MeeEEEEEEEEEEEEE" …
Run Code Online (Sandbox Code Playgroud) 我有一点时间通过Intent
/ Bundle
s 搞清楚我的方法会有什么数据.我已经尝试添加断点来检查数据,但我没有看到任何东西.也许是因为Parcelable
我无法在Eclipse中手动读取它.
例如,a onActivityResult(int requestCode, int resultCode, Intent data)
表示a Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI)
.我如何知道可用的数据?请注意,我不是要求提供哪些数据,但我是如何理解的,所以我可以将相同的想法应用于任何Bundle
/ Intent
来自Android框架?也许这看起来像文档一样简单,但我没有看到完整的数据列表,我在Eclipse中看不到它.所以我迷路了.
为什么ViewGroup
唯一得到ACTION_DOWN
的onInterceptTouchEvent
?根据文档,只要返回false,它就应该接收所有事件类型.
http://developer.android.com/reference/android/view/ViewGroup.html#onInterceptTouchEvent%28android.view.MotionEvent%29
第3点.
示例代码:
public class MainActivity extends Activity {
private static final String TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new Container(this));
}
private class Container extends LinearLayout {
public Container(Context context) {
super(context);
setBackgroundColor(0xFF0000FF);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
Log.i(TAG, "onInterceptTouchEvent");
int action = ev.getActionMasked();
switch (action) {
case MotionEvent.ACTION_DOWN:
Log.i(TAG, "onInterceptTouchEvent.ACTION_DOWN");
break;
case MotionEvent.ACTION_MOVE:
Log.i(TAG, "onInterceptTouchEvent.ACTION_MOVE");
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
Log.i(TAG, "onInterceptTouchEvent.ACTION_UP");
break;
}
return …
Run Code Online (Sandbox Code Playgroud) 谁/什么叫onFinishInflate()?无论我如何夸大我的布局文件(在代码中),这种方法似乎永远不会被触发.任何人都可以给我一个例子或告诉我什么时候onFinishInflate()实际上被调用?
根据Romain Guy的博客文章Android Performance Case Study在谈到Overdraw时,他说:
删除窗口背景:系统使用主题中定义的背景在启动应用程序时创建预览窗口.除非您的应用程序是透明的,否则永远不要将其设 相反,通过调用getWindow()将其设置为您想要的颜色/图像或从onCreate()中删除.setBackgroundDrawable(null).***
但是getWindow().setBackgroundDrawable(null)似乎没有任何效果.这是一个代码示例:
//MainActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().setBackgroundDrawable(null);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
// main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:background="#FFE0FFE0"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:background="#FFFFFFE0" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text="@string/hello_world" />
</LinearLayout>
// styles.xml
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:windowBackground">@color/yellow</item>
</style>
Run Code Online (Sandbox Code Playgroud)
此示例在图像中生成结果.您可以看到外层有一个透支,窗口背景颜色仍然可见.我预计窗口的背景会消失,只有lineralayout才能透支.
我有这样的抽象:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:state_window_focused="true"
android:drawable="@drawable/seek_thumb_pressed" />
<item android:state_focused="true"
android:state_window_focused="true"
android:drawable="@drawable/seek_thumb_selected" />
<item android:state_selected="true"
android:state_window_focused="true"
android:drawable="@drawable/seek_thumb_selected" />
<item android:drawable="@drawable/seek_thumb_normal" />
Run Code Online (Sandbox Code Playgroud)
在代码中,如何设置Drawable的特定状态?我想将它设置为state_pressed = true状态.
我有这个容器类,用于制作其中一个尺寸,宽度或高度,另一个尺寸的比例.例如,我想要一个16:9的布局容器,其宽度为"match_parent".但是,当使用高度作为"match_parent"时,Android似乎没有正确地重新布局.当我将高度设置为宽度的比例时,一切都很好!反之亦然,它不起作用.这是怎么回事?
public class RatioLayout extends ViewGroup {
private float ratio = 1.6222f; // 4 x 3 default. 1.78 is 16 x 9
public float getRatio() { return ratio; }
public void setRatio(float ratio) {
this.ratio = ratio;
requestLayout();
}
public RatioLayout(Context context) {
this(context, null);
}
public RatioLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public RatioLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Log.i("Ratio", "/onMeasure");
int width …
Run Code Online (Sandbox Code Playgroud) 我有一个php webservice,可以调用(从手机)来执行某些任务.要完成这些任务,调用者必须"登录".处理身份验证的最佳方法是什么?
目前,我只是在使用SESSIONS.客户端调用登录API以及所需的任何其他API.但是我担心让20万人都打电话给这项服务并参加所有会议.我不确定服务器将如何响应.有小费吗?这通常如何处理?像facebook,flickr等....