所以,我的第一个主要应用程序几乎已编码,我正在对我的代码进行优化.该应用程序工作正常,但我不确定我将上下文传递给其他类的方式.我不想以错误的方式去做.我在Stackoverflow中偶然发现了关于上下文的文章和问题,这是将它传递给非活动类的正确方法.我也阅读了文档,但作为一名芬兰人,使得复杂的技术更难以理解.
所以,一个简单的问题.我将主要活动的上下文传递给其他(帮助者)类的方式是否正确?如果没有,我在哪里可以阅读更多关于这些情况的更好实践.
例如:MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle sis){
super(sis);
new Helper(MyActivity.this).makeMyAppAwesome();
}
}
Run Code Online (Sandbox Code Playgroud)
Helper.java
public class Helper {
Context context;
Helper(Context ctx){
this.context = ctx;
}
public void makeMyAppAwesome(){
makeBaconAndEggsWithMeltedCheese(context);
}
}
Run Code Online (Sandbox Code Playgroud)
这个可以吗?如果有人可以提供一个易于阅读的文章,并提供有关此主题的示例,那就太好了.
所以,我有这个"通知"屏幕,显示用户的通知.当导航到此屏幕时,它将变为空白,因为通知是从后端API实时加载的.
这里有一些代码来说明问题:
class _MyItemsPageState extends State<MyItemsPage> {
final GlobalKey<RefreshIndicatorState> _refreshIndicatorKey =
new GlobalKey<RefreshIndicatorState>();
List<MyItem> _items = [];
@override
void initState() {
super.initState();
// Nothing is displaying on screen initially, since the items are loaded from API on startup.
// Preferably in this state, the refresh indicator would be shown while the items load.
// It's not currently possible in this place, since it seems that the Widget hasn't been built yet.
_refreshIndicatorKey.currentState.show(); // currentState null at this time, so the …Run Code Online (Sandbox Code Playgroud) 所以我想制作一个加载了全部功能的免费应用程序.在应用程序检测到许可的专业密钥之前,专业版功能将被禁用.当然我想让pro key使用LVL检查它的许可证.虽然我知道如何在此之前做正确的事情,但我不知道如何使专业密钥与应用程序通信它应该启用专业功能.
这是主应用程序代码(com.test.mainapp):
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context = getApplicationContext();
final PackageManager pacman = getPackageManager();
final int signatureMatch = pacman.checkSignatures(getPackageName(),
"com.test.mainapp_key");
if (signatureMatch == PackageManager.SIGNATURE_MATCH) {
Toast.makeText(context, "Pro key detected!", Toast.LENGTH_SHORT)
.show();
} else {
Toast.makeText(context, "Free version", Toast.LENGTH_SHORT).show();
}
}
Run Code Online (Sandbox Code Playgroud)
虽然这会阻止其他人为我的应用程序制作假钥匙,但他们仍然可以在线共享关键应用程序,并且它可以正常工作.因为我们不能从另一个应用程序执行LVL检查,我希望许可证密钥应用程序检查它自己的许可证,如果它是正确的,那么只有用户才能获得专业功能.如何让许可证密钥应用程序与主应用程序相互通信?
我试图获得的功能就像Titanium Backup一样.
所以我想在ProgressDialog中添加一个自定义的imageview:
LinearLayout root = (LinearLayout)findViewById(R.id.root);
ProgressDialog pdialog = new ProgressDialog(context);
pdialog.setTitle("Wait");
pdialog.setMessage("Loading...");
pdialog.addContentView((ImageView)findViewById(R.id.imageV3iew1), root.getLayoutParams());
pdialog.setCancelable(false);
pdialog.show();
Run Code Online (Sandbox Code Playgroud)
它说指定的子节点已经有父节点,我必须首先在父节点上调用removeView.我知道如何创建自定义对话框,但我想将我的imageview直接附加到ProgressDialog.我需要提供哪种功能?
我想从偏好中获得一个浮动.当用户没有输入任何内容时,这可以正常工作,但是当在框中输入8.23并保存它时,应用程序会在下次启动时崩溃.
MainActivity.java:
float hourly_rate;
SharedPreferences userdata;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
userdata = PreferenceManager.getDefaultSharedPreferences(this);
hourly_rate = userdata.getFloat("hourly_rate", 0.0f);
}
Run Code Online (Sandbox Code Playgroud)
的preferences.xml:
<PreferenceCategory
android:title="@string/general_preferences_title">
<EditTextPreference
android:inputType="numberDecimal"
android:key="hourly_rate"
android:title="@string/hourly_rate_title"
android:summary="@string/hourly_rate_summary" />
</PreferenceCategory>
Run Code Online (Sandbox Code Playgroud)
当我删除getFloat行时,应用程序不会强制关闭.
我不知道这里有什么问题.
Activity.java:
...
Intent intent = new Intent(Activity.this, Service.class);
intent.putExtra(Service.KEY_TEST, "123456789");
startService(intent);
...
Run Code Online (Sandbox Code Playgroud)
Service.java:
...
private Intent intent;
public static final String KEY_TEST;
@Override
public void onCreate() {
super.onCreate();
Log.d("TEST", intent.getStringExtra(KEY_TEST)); // when I remove this line,
// it works, otherwise gives NullPointerException and FC's
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
this.intent = intent;
return START_STICKY;
}
...
Run Code Online (Sandbox Code Playgroud)
该服务显然没有收到活动发送的额外内容.当我尝试获取之前发送的任何额外内容时,app force关闭,LogCat给出NullPointerException.当我删除我尝试获取额外内容的行时,应用程序不会强行关闭,但我显然也没有收到附加内容.