我似乎无法在android中声明TelephonyManager,这正是我想要做的:
TelephonyManager tele = Context.getSystemService(Context.TELEPHONY_SERVICE);
Run Code Online (Sandbox Code Playgroud)
我这样做了吗?
我在我的android项目的R.strings资源中定义了很多常量字符串.但是,我想在JUST一个java文件中使用它们.这个类只是一个java类.反正有没有得到这个参考?
我能够引用它,但我无法使用该Context.getString()方法,因为它只是一个java文件.
我有什么选择?我应该在这个java文件中定义所有常量字符串吗?或者我还可以使用R.strings吗?
例
getString(R.string.PLUS_SERVICE))
这不起作用,因为我没有上下文.
谢谢!
在我的SQLite安装程序类中,我有一个DbHelper,它需要一个Context作为它的一部分.要设置上下文,我只在我的SQLite类中使用一个构造函数,它需要一个上下文作为参数的一部分.
但我刚遇到一个问题.当我试图从一个不是Activity的类中调用我的SQLite类时,我无法使用context classname.this它,它正在困扰我.
我也尝试这样做来声明一个上下文:
protected Context context;
Run Code Online (Sandbox Code Playgroud)
然后再打电话给它:
SetSql PlayerObject = new SetSql(This.context);
Run Code Online (Sandbox Code Playgroud)
但这也不起作用.
有什么建议吗?
android中的db.execSQL()方法和activityObject.managedQuery()方法有什么区别?
这是一个非常愚蠢的问题,但我在Context context =中设置了上下文等于...
只是声明它将它设置为null,我需要为我的应用程序使用上下文.
我有App一个包含我的上下文的课程Application.但是当我编译时,我error在这一行的其他课程中得到了一个:
App app = (App) getApplication();
Run Code Online (Sandbox Code Playgroud)
课程App:
import android.app.Application;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.example.Radio_KPI.R;
import com.example.Radio_KPI.Utils.Const;
public class App extends Application {
private MediaPlayer player;
private NotificationManager manager;
private String curSong;
private Context Main_con;
public Context getMain_con() {
return Main_con;
}
public void setMain_con(Context main_con) {
Main_con = main_con;
}
public String getCurSong() {
return curSong;
}
public void …Run Code Online (Sandbox Code Playgroud) 在以下onReceive方法中传递的上下文是什么BroadcastReciver:
public void onReceive (Context context, Intent intent)
Run Code Online (Sandbox Code Playgroud)
根据官方文件:
接收器运行的上下文.
我已经阅读了几篇关于将上下文传递给适配器或其他内容的文章,并且我为获取应用程序上下文做了一些contextholder:
import android.content.Context;
public class ContextHolder {
private static ContextHolder ourInstance = new ContextHolder();
private Context context;
public static ContextHolder getInstance() {
return ourInstance;
}
private ContextHolder() {
context = null;
}
public void setContext(Context context){
this.context = context;
}
public Context getApplicationContext(){
return context;
}
}
Run Code Online (Sandbox Code Playgroud)
然后在MainActivity中创建ContextHolder对象并设置上下文,如下所示:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ContextHolder contextHolder = ContextHolder.getInstance();
contextHolder.setContext(this.getApplicationContext());
}
Run Code Online (Sandbox Code Playgroud)
在其他一些类中,我需要使用contex:
ContextHolder contextHolder = ContextHolder.getInstance();
Resources resources = contextHolder.getApplicationContext().getResources();
Run Code Online (Sandbox Code Playgroud)
问题是,我做对了吗?它会导致内存泄漏或其他令人讨厌的东西吗?
我的目标是拥有一个后台服务,每天更新两次相当大量的数据(更新约5分钟,可能更多).
所以我有一个启动此服务的GcmTaskService:
public class SyncOfflineCoursesService extends Service {
private final IBinder mBinder = new MonBinder();
private final SharedPreferenceManagerToReplace sharedPreferenceManager;
public SyncOfflineCoursesService() {
sharedPreferenceManager = new SharedPreferenceManagerToReplace(this); //crash on this line
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
...
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
public class MonBinder extends Binder {
SyncOfflineCoursesService getService() {
return SyncOfflineCoursesService.this;
}
}
}
Run Code Online (Sandbox Code Playgroud)
SharedPreferenceManagerToReplace :
public class SharedPreferenceManagerToReplace {
private final SharedPreferences prefs;
public SharedPreferenceManagerToReplace(Context context) { …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个即使我的应用程序关闭也能运行的服务.但是,我需要在此服务中使用我的应用上下文.当应用程序运行时,该服务也可以正常工作,但是当我关闭应用程序(调用onDestroy())时,始终返回.getContext()null
服务
public class SubscribeService extends Service {
private Context context;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
context = this; //Returns null when service is running on background
context = MyApp.getContext(); //Also null
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//do stuff using context
}
Run Code Online (Sandbox Code Playgroud)
MyApp的
public class MyApp extends Application {
private static Context context;
public static Context …Run Code Online (Sandbox Code Playgroud)