有人可以告诉我一个IntentService可以用a无法完成的事情的例子Service(反之亦然)吗?
我也相信IntentService在不同的线程中运行而Service不是.所以,据我所知,在自己的线程中启动服务就像开始一样IntentService.不是吗?
如果有人可以帮我解决我的两个问题,我将不胜感激.
multithreading android android-service android-intentservice
在我的项目中,我有一个EditText.我想计算中的字符数EditText,并在数字中显示该数字TextView.我写了以下代码,它工作正常.但是,我的问题是当我点击Backspace它计数时,但我需要减少数量.我怎么考虑Backspace?
tv = (TextView)findViewById(R.id.charCounts);
textMessage = (EditText)findViewById(R.id.textMessage);
textMessage.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable s) {
i++;
tv.setText(String.valueOf(i) + " / " + String.valueOf(charCounts));
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count){}
});
Run Code Online (Sandbox Code Playgroud) 我正在寻找一些开源Java项目来进入Java,并注意到它们中有很多都有某种"常量"接口.
例如,processing.org有一个名为PConstants.java的接口,大多数其他核心类实现了这个接口.界面充满了静态成员.有这种方法的理由,还是这被认为是不好的做法?为什么不在有意义的地方使用枚举,或者使用静态类?
我发现使用接口允许某种伪"全局变量"很奇怪.
public interface PConstants {
// LOTS OF static fields...
static public final int SHINE = 31;
// emissive (by default kept black)
static public final int ER = 32;
static public final int EG = 33;
static public final int EB = 34;
// has this vertex been lit yet
static public final int BEEN_LIT = 35;
static public final int VERTEX_FIELD_COUNT = 36;
// renderers known to processing.core
static final …Run Code Online (Sandbox Code Playgroud) 我正在开发一个使用Web服务从服务器获取数据的Android应用程序,因为我有三套不同的URL来指向开发系统,测试服务器和实时服务器.每当我想提供测试/实时应用程序时,很难更改URL.所以我计划将其设置为可配置,以便应用程序可以根据我的构建类型配置常量获取适当的URL.所以,
例如:xml资源
<integer name="config_build_type">0</integer>
<string-array name="url_authentication">
<item >http://development.com/xxxx</item>
<item >http://test.com/xxx</item>
<item >http://example.com/xxx</item>
</string-array>
Run Code Online (Sandbox Code Playgroud)
Java静态常量
public class Config {
public static final int BUILD_TYPE = 0; // 0 - development, 1 - test, 2 - live
public static final String[] URL_AUTHENTICATION = {"http://development.com/", "http://test.com/", "http://example.com"};
}
Run Code Online (Sandbox Code Playgroud) 我有一个相当大的Android应用程序,它依赖于许多库项目.Android编译器对每个.dex文件有65536个方法的限制,我超过了这个数字.
当你达到方法限制时,你可以选择两种途径(至少我知道).
1)缩小你的代码
2)构建多个dex文件(参见此博文)
我调查了两个并试图找出导致我的方法计数变得如此之高的原因.Google Drive API占据了Guava依赖的最大块,超过12,000.Drive API v2的总库存超过23,000!
我想我的问题是,你认为我该怎么做?我应该删除Google云端硬盘集成作为我应用的功能吗?有没有办法缩小API(是的,我使用proguard)?我应该采用多重dex路由(看起来相当痛苦,尤其是处理第三方API)?
我在这里听到了关于这个错误的许多答案,但我的问题没有解决.每次我运行我的应用程序时都没有广告出现,我在Logcat中收到此错误消息:
"获取广告响应时出现问题.错误代码:0无法加载广告:0"
这是我的java代码.我错过了什么?
private void LoadAds() {
AdView mAdView = (AdView) findViewById(R.id.linearLayoutAdmob);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
Run Code Online (Sandbox Code Playgroud)
在这里我的xml布局与最后一行的admob
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/background_play"
tools:context=".MainActivity" >
<RelativeLayout
android:id="@+id/rlayout1"
android:layout_width="fill_parent"
android:layout_height="0px"
android:padding="5dip"
android:layout_weight="2.3" >
<com.aapps.find.TouchImage
android:id="@+id/gameimage1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:src="@drawable/ic_launcher" />
<ImageView
android:id="@+id/hitimage1"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/hit"
android:visibility="invisible" />
<ImageView
android:id="@+id/errorimage1"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/miss1"
android:visibility="invisible" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/rlayoutprogress"
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_weight="0.1" >
<com.aapps.find.utility.SaundProgressBar
android:id="@+id/progressBar"
style="@style/Widget.ProgressBar.RegularProgressBar"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/rlayout2"
android:padding="5dip"
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_weight="2.3" >
<com.aapps.find.TouchImage
android:id="@+id/gameimage2"
android:layout_width="fill_parent" …Run Code Online (Sandbox Code Playgroud) 我正在处理通知,我必须使用setLatestEventInfo.但是,Android Studio显示以下错误消息:
无法解析方法setLatestEventinfo
这是我的代码片段:
private void createNotification(Context context, String registrationID) {
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.icon,"Registration Successfull",System.currentTimeMillis());
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Intent intent = new Intent(context,RegistrationResultActivity.class);
intent.putExtra("registration_ID",registrationID);
PendingIntent pendingIntent = PendingIntent.getActivity(context,0,intent,0);
notification.setLatestEventInfo(context,"Registration","Successfully Registered",pendingIntent);
}
Run Code Online (Sandbox Code Playgroud)
或者,如果他们是另一种方式,请善意地告诉我.
我正在尝试检测何时安装新的应用程序但仅在我的应用程序正在运行时.我设法通过制作BroadcastReceiver并在AndroidManifest文件中激活它来检测应用程序的安装,但即使我的应用程序已关闭,这也会检测到.这就是为什么我需要手动激活和停用broadcastreveiver.要做到这一点,我有这个代码:
br = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.i("Enter", "Enters here");
Toast.makeText(context, "App Installed!!!!.", Toast.LENGTH_LONG).show();
}
};
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
intentFilter.addAction(Intent.ACTION_PACKAGE_INSTALL);
registerReceiver(br, intentFilter);
Run Code Online (Sandbox Code Playgroud)
这应该在安装新应用程序时举杯.但遗憾的是,事实并非如此.它不会进入onReceive方法.任何帮助表示赞赏.
我在Android项目的"assets"文件夹中有一些带有HTML文件的文件夹.我需要从列表中的资产子文件夹中显示这些HTML文件.我已经写了一些关于制作这个列表的代码.
lv1 = (ListView) findViewById(R.id.listView);
// Insert array in ListView
// In the next row I need to insert an array of strings of file names
// so please, tell me, how to get this array
lv1.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, filel));
lv1.setTextFilterEnabled(true);
// onclick items in ListView:
lv1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
//Clicked item position
String itemname = new Integer(position).toString();
Intent intent = new Intent();
intent.setClass(DrugList.this, Web.class);
Bundle b = new Bundle(); …Run Code Online (Sandbox Code Playgroud) 我需要创建一个临时文件,所以我尝试了这个:
String[] TempFiles = {"c1234c10","c1234c11","c1234c12","c1234c13"};
for (int i = 0; i <= 3; i++) {
try {
String tempFile = TempFiles[i];
File temp = File.createTempFile(tempFile, ".xls");
System.out.println("Temp file : " + temp.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
输出是这样的:
Temp file : C:\Users\MD1000\AppData\Local\Temp\c1234c108415816200650069233.xls
Temp file : C:\Users\MD1000\AppData\Local\Temp\c1234c113748833645638701089.xls
Temp file : C:\Users\MD1000\AppData\Local\Temp\c1234c126104766829220422260.xls
Temp file : C:\Users\MD1000\AppData\Local\Temp\c1234c137493179265536640669.xls
Run Code Online (Sandbox Code Playgroud)
现在,我不希望将额外的数字(long int)添加到文件名中.我怎样才能做到这一点?谢谢