美文网首页
2018-04-26 Application name属性

2018-04-26 Application name属性

作者: JOJO大魔王 | 来源:发表于2018-04-26 10:39 被阅读0次

    最近做机顶盒开发,需要从开机广播中获取一个“userName”的值放入到Application中。测试过程发现怎么也获取不到,抓了下Log发现自定义的Application没有启动。查了下资料才发现是因为没有设置Application的name属性,导致没有关联自定义的Application。

    Receiver代码:

    @Override
        public void onReceive(Context context, Intent intent) {
            // TODO: This method is called when the BroadcastReceiver is receiving
            SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            Gson gson = new Gson();
            Map result = (Map) gson.fromJson(intent.getStringExtra("params"), Map.class);
            editor.putString("userName",(String)result.get("userName"));
            System.out.println("MyReceiver : " + (String)result.get("userName"));
            editor.commit();
        }
    

    Application代码:

    sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
            userName = sharedPreferences.getString("userName","");
            System.out.println("MyAppliaction onActivityStarted : " + userName);
    

    AndroidManifest.xml:

    <application
            android:name=".MyApplication"
            android:allowBackup="true"
    ...
    />
    

    相关文章

      网友评论

          本文标题:2018-04-26 Application name属性

          本文链接:https://www.haomeiwen.com/subject/krrplftx.html