做了个流量记录的程序,但是这个姑娘没有实现,也不知道怎么做,START_STICKY等FLAG不知道怎么用?
求帮助,谢谢大家,service主要代码如下
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
Log.v("TrafficService", "Bind");
state = BINLD;
//使用线程,该线程不会被销毁
return myBinder;
}
class TrafficBinder extends Binder {
public float his = history;
TrafficService getService(){
return TrafficService.this;
}
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
receiver = new NetReceiver();
IntentFilter nfilter=new IntentFilter();
nfilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
this.registerReceiver(receiver, nfilter);
dealBuffer();
handler.post(createBrod);
Log.v("TrafficService", "Create "+history+" "+todayTraffic+" "+tempTraffic);
super.onCreate();
}
@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
state = UNBINLD;
Log.v("TrafficService", "Unbind");
return true;
}
@Override
public void onRebind(Intent intent) {
// TODO Auto-generated method stub
Log.v("TrafficService", "Rebind");
state = BINLD;;
super.onRebind(intent);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
handler.removeCallbacks(updateBrod);
momorybuffer();
nManger.cancel(R.layout.notifi);
unregisterReceiver(receiver);
Log.v("TrafficService", "destroy "+history+" "+todayTraffic+" "+tempTraffic);
super.onDestroy();
}
@Override
public boolean stopService(Intent name) {
// TODO Auto-generated method stub
momorybuffer();
nManger.cancel(R.layout.notifi);
Log.v("TrafficService",name.getAction());
Log.v("TrafficService", "stop "+history+" "+todayTraffic+" "+tempTraffic);
return super.stopService(name);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Log.v("TrafficService","startCommand");
flags = START_STICKY;
return super.onStartCommand(intent, flags, startId);
// return START_REDELIVER_INTENT;
}
【翻译】(50)compatible-screens元素
see
http://developer.android.com/guide/topics/manifest/compatible-screens-element.html
原文见
http://developer.android.com/guide/topics/manifest/compatible-screens-element.html
-------------------------------
<compatible-screens>
compatible-screens元素
-------------------------------
* syntax:
* 语法:
-------------------------------
<compatible-screens>
<screen android:screenSize=["small" | "normal" | "large" | "xlarge"]
android:screenDensity=["ldpi" | "mdpi" | "hdpi" | "xhdpi"] />
...
</compatible-screens>
-------------------------------
* contained in:
* 被包含在:
<manifest>
* description:
* 描述:
Specifies each screen configuration with which the application is compatible. Only one instance of the <compatible-screens> element is allowed in the manifest, but it can contain multiple <screen> elements. Each <screen> element specifies a specific screen size-density combination with which the application is compatible.
指定应用程序兼容的每个屏幕属性。在清单中只允许有一个<compatible-screens>元素的实例,但它可以包含多个<screen>元素。每个<screen>元素指定该应用程序兼容的一种特定的屏幕大小-密度组合。
The Android system does not read the <compatible-screens> manifest element (neither at install-time nor at runtime). This element is informational only and may be used by external services (such as Android Market) to better understand the application's compatibility with specific screen configurations and enable filtering for users. Any screen configuration that is not declared in this element is a screen with which the application is not compatible. Thus, external services (such as Android Market) should not provide the application to devices with such screens.
Android系统不会读取<compatible-screens>清单元素(也不会在安装期或运行期读取)。这个元素只是信息,而且可能被外部服务使用(诸如Android市场)以更好地理解应用程序对特定屏幕配置的兼容性和为用户使能过滤。在这个元素中没有声明的任意屏幕配置是该应用程序不兼容的一种屏幕。因此,外部服务(诸如Android市场)不应该提供该应用程序给带有这些屏幕的设备。
-------------------------------
Caution: Normally, you should not use this manifest element. Using this element can dramatically reduce the potential user base for your application, by not allowing users to install your application if they have a device with a screen configuration that you have not listed. You should use it only as a last resort, when the application absolutely does not work with all screen configurations. Instead of using this element, you should follow the guide to Supporting Multiple Screens, in order to provide complete support for multiple screens, by adding alternative resources for different screen sizes and densities.
警告:通常,你不应该使用这个清单元素。使用这个元素可以显著地降低你的应用程序的潜在用户基线,通过不允许用户安装你的应用程序如果他们拥有一个带有你不曾列举的屏幕配置的设备。你应该只使用它作为最后的手段,当应用程序绝对不能与所有屏幕配置一起工作时。不是使用这个元素,你而是应该遵循指引支持多屏幕,以便提供对多屏幕的完整支持,通过添加用于不同屏幕大小和密度的可选资源来做到。
-------------------------------
If you want to set only a minimum screen size for your your application, then you should use the <supports-screens> element. For example, if you want your application to be available only for large and xlarge screen devices, the <supports-screens> element allows you to declare that your application does not support small and normal screen sizes. External services (such as Android Market) will filter your application accordingly. You can also use the <supports-screens> element to declare whether the system should resize your application for different screen sizes.
如果你只是希望为你的应用程序设置一个最小屏幕大小,那么你应该使用<supports-screens>元素。例如,如果你希望你的应用程序只对大和超大屏幕设备可用,那么<supports-screens>元素允许你声明你的应用程序不支持小和普通屏幕大小。外部服务(诸如Android市场)将据此过滤你的应用程序。你还可以使用<supports-screens>元素来声明系统是否应该为不同的屏幕大小重新改变你的应用程序的大小。
Also see the Market Filters document for more information about how Android Market filters applications using this and other manifest elements.
另见市场过滤器文档以获得关于Android市场过滤器应用程序如何使用这个和其它清单元素的更多信息。
* child elements:
* 子元素:
* <screen>
Specifies a single screen configuration with which the application is compatible.
指定该应用程序兼容的单一屏幕配置。
At least one instance of this element must be placed inside the <compatible-screens> element. This element must include both the android:screenSize and android:screenDensity attributes (if you do not declare both attributes, then the element is ignored).
这个元素至少有一个实例必须放在<compatible-screens>元素内。这个元素必须都包含android:screenSize和android:screenDensity属性(如果你不同时声明这两个属性,那么该元素被忽略)。
* attributes:
* 属性:
* android:screenSize
Required. Specifies the screen size for this screen configuration.
必需的。为这个屏幕配置指定屏幕大小。
Accepted values:
接受的值
* small
* normal
* large
* xlarge
For information about the different screen sizes, see Supporting Multiple Screens.
想获得关于不同屏幕大小的信息,参见支持多屏幕。
* android:screenDensity
Required. Specifies the screen density for this screen configuration.
必需的。指定用于这个屏幕配置的屏幕密度。
Accepted values:
接受的值:
* ldpi
* mdpi
* hdpi
* xhdpi
For information about the different screen densities, see Supporting Multiple Screens.
想获得关于不同屏幕密度的信息,参见支持多屏幕。
* example
* 示例
If your application is compatible with only small and normal screens, regardless of screen density, then you must specify eight different <screen> elements, because each screen size has four different density configurations. You must declare each one of these; any combination of size and density that you do not specify is considered a screen configuration with which your application is not compatible. Here's what the manifest entry looks like if your application is compatible with only small and normal screens:
如果你的应用程序只兼容小和普通屏幕,不管屏幕密度如何,那么你必须指定不同的<screen>元素之一,因为每个屏幕大小拥有四种不同的密度配置。你必须声明它们之中的一个;你没有指定的大小和密度的任意组合被认为是你的应用程序不兼容的屏幕配置。这里是清单条目看起来的样子,如果你的应用程序只兼容小和普通屏幕:
-------------------------------
<manifest ... >
...
<compatible-screens>
<!-- all small size screens -->
<!-- 所有小尺寸的屏幕 -->
<screen android:screenSize="small" android:screenDensity="ldpi" />
<screen android:screenSize="small" android:screenDensity="mdpi" />
<screen android:screenSize="small" android:screenDensity="hdpi" />
<screen android:screenSize="small" android:screenDensity="xhdpi" />
<!-- all normal size screens -->
<!-- 所有普通大小的屏幕 -->
<screen android:screenSize="normal" android:screenDensity="ldpi" />
<screen android:screenSize="normal" android:screenDensity="mdpi" />
<screen android:screenSize="normal" android:screenDensity="hdpi" />
<screen android:screenSize="normal" android:screenDensity="xhdpi" />
</compatible-screens>
<application ... >
...
<application>
</manifest>
-------------------------------
* introduced in:
* 引入:
API Level 9
API级别9
* see also:
* 另见:
Supporting Multiple Screens
支持多屏幕
Market Filters
市场过滤器
Except as noted, this content is licensed under Apache 2.0. For details and restrictions, see the Content License.
除特别说明外,本文在Apache 2.0下许可。细节和限制请参考内容许可证。
Android 4.0 r1 - 10 Feb 2012 0:44
-------------------------------
Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.
(此页部分内容基于Android开源项目,以及使用根据创作公共2.5来源许可证描述的条款进行修改)
(本人翻译质量欠佳,请以官方最新内容为准,或者参考其它翻译版本:
* ソフトウェア技術ドキュメントを勝手に翻訳
http://www.techdoctranslator.com/android
* Ley's Blog
http://leybreeze.com/blog/
* 农民伯伯
http://www.cnblogs.com/over140/
* Android中文翻译组
http://androidbox.sinaapp.com/
)
今天讲一下Android中Intent中如何传递对象,就我目前所知道的有两种方法,一种是Bundle.putSerializable(Key,Object);另一种是Bundle.putParcelable(Key, Object);当然这些Object是有一定的条件的,前者是实现了Serializable接口,而后者是实现了Parcelable接口
第一步:新建一个Android工程命名为IntentDemo
第二步:修改main.xml布局文件
<?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" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Welcome to Mr wei's blog." /> <Button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Serializable" /> <Button android:id="@+id/button2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Parcelable" /> </LinearLayout>
第三步:新建两个类一个是Person.java实现Serializable接口,另一个Book.java实现Parcelable接口,代码分别如下:
Person.java
package cn.caiwb.intent; import java.io.Serializable; public class Person implements Serializable { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
Book.java
package cn.caiwb.intent; import android.os.Parcel; import android.os.Parcelable; public class Book implements Parcelable { private String bookName; private String author; private int publishTime; public String getBookName() { return bookName; } public void setBookName(String bookName) { this.bookName = bookName; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public int getPublishTime() { return publishTime; } public void setPublishTime(int publishTime) { this.publishTime = publishTime; } public static final Parcelable.Creator<Book> CREATOR = new Creator<Book>() { public Book createFromParcel(Parcel source) { Book mBook = new Book(); mBook.bookName = source.readString(); mBook.author = source.readString(); mBook.publishTime = source.readInt(); return mBook; } public Book[] newArray(int size) { return new Book[size]; } }; public int describeContents() { return 0; } public void writeToParcel(Parcel parcel, int flags) { parcel.writeString(bookName); parcel.writeString(author); parcel.writeInt(publishTime); } }
第四步:修改IntentDemo.java,并且新建两个Activity,一个是IntentDemo1.java,别一个是IntentDemo2.java.分别用来显示Person对像数据,和Book对象数据:,代码分别如下:
IntentDemo.java
package cn.caiwb.intent; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class IntentDemo extends Activity implements OnClickListener { private Button sButton,pButton; public final static String SER_KEY = "cn.caiwb.intent.ser"; public final static String PAR_KEY = "cn.caiwb.intent.par"; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); setupViews(); } public void setupViews(){ sButton = (Button)findViewById(R.id.button1); pButton = (Button)findViewById(R.id.button2); sButton.setOnClickListener(this); pButton.setOnClickListener(this); } //Serializeable传递对象的方法 public void SerializeMethod(){ Person mPerson = new Person(); mPerson.setName("frankie"); mPerson.setAge(25); Intent mIntent = new Intent(this,IntentDemo1.class); Bundle mBundle = new Bundle(); mBundle.putSerializable(SER_KEY,mPerson); mIntent.putExtras(mBundle); startActivity(mIntent); } //Pacelable传递对象方法 public void PacelableMethod(){ Book mBook = new Book(); mBook.setBookName("Android Tutor"); mBook.setAuthor("Frankie"); mBook.setPublishTime(2010); Intent mIntent = new Intent(this,IntentDemo2.class); Bundle mBundle = new Bundle(); mBundle.putParcelable(PAR_KEY, mBook); mIntent.putExtras(mBundle); startActivity(mIntent); } //铵钮点击事件响应 public void onClick(View v) { if(v == sButton){ SerializeMethod(); }else{ PacelableMethod(); } } }
IntentDemo1.java
package cn.caiwb.intent; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class IntentDemo1 extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView mTextView = new TextView(this); Person mPerson = (Person)getIntent().getSerializableExtra(ObjectTranDemo.SER_KEY); mTextView.setText("You name is: " + mPerson.getName() + "/n"+ "You age is: " + mPerson.getAge()); setContentView(mTextView); } }
IntentDemo2.java
package cn.caiwb.intent; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class IntentDemo2 extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView mTextView = new TextView(this); Book mBook = (Book)getIntent().getParcelableExtra(ObjectTranDemo.PAR_KEY); mTextView.setText("Book name is: " + mBook.getBookName()+"/n"+ "Author is: " + mBook.getAuthor() + "/n" + "PublishTime is: " + mBook.getPublishTime()); setContentView(mTextView); } }
OK 可以了~