小编Gop*_*rvi的帖子

示例:使用Messaging在活动和服务之间进行通信

我找不到任何关于如何在活动和服务之间发送消息的示例,我花了太多时间来搞清楚这一点.这是一个供其他人参考的示例项目.

此示例允许您直接启动或停止服务,并单独绑定/取消绑定服务.当服务运行时,它会以10 Hz的频率递增一个数字.如果活动绑定到Service,则会显示当前值.数据作为整数和字符串传输,因此您可以看到如何以两种不同的方式进行操作.活动中还有按钮将消息发送到服务(更改增量值).

截图:

Android服务消息示例的屏幕截图

AndroidManifest.xml中:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.exampleservice"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MainActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    <service android:name=".MyService"></service>
    </application>
    <uses-sdk android:minSdkVersion="8" />
</manifest>
Run Code Online (Sandbox Code Playgroud)

水库\值\ strings.xml中:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">ExampleService</string>
    <string name="service_started">Example Service started</string>
    <string name="service_label">Example Service Label</string>
</resources>
Run Code Online (Sandbox Code Playgroud)

水库\布局\ main.xml中:

<RelativeLayout
    android:id="@+id/RelativeLayout01"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <Button
        android:id="@+id/btnStart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start Service" >
    </Button>

    <Button
        android:id="@+id/btnStop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="Stop Service" >
    </Button>
</RelativeLayout>

<RelativeLayout …
Run Code Online (Sandbox Code Playgroud)

android android-service android-activity

580
推荐指数
5
解决办法
28万
查看次数

Android Facebook 4.0 SDK如何获取电子邮件,出生日期和用户性别

我使用以下代码.我想要用户的出生日期,电子邮件和性别.请帮忙.如何检索这些数据?

这是我onViewCreated()在片段中的内容.

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {

    // Setup TextView.
    mTextDetails = (TextView) view.findViewById(R.id.text_details);

    // Set up Login Button.
    LoginButton mButtonLogin = (LoginButton) view.findViewById(R.id.login_button);
    // setFragment only if you are using it inside a Fragment.
    mButtonLogin.setFragment(this);
    mButtonLogin.setReadPermissions("user_friends");
    mButtonLogin.setReadPermissions("public_profile");
    mButtonLogin.setReadPermissions("email");
    mButtonLogin.setReadPermissions("user_birthday");

    // Register a callback method when Login Button is Clicked.
    mButtonLogin.registerCallback(mCallbackManager, mFacebookCallback);

}
Run Code Online (Sandbox Code Playgroud)

这是我的回调方法.

private FacebookCallback<LoginResult> mFacebookCallback = new FacebookCallback<LoginResult>() {
    @Override
    public void onSuccess(LoginResult loginResult) {
        Log.d("Shreks Fragment", "onSuccess");


        Profile profile = Profile.getCurrentProfile();
        Log.d("Shreks …
Run Code Online (Sandbox Code Playgroud)

android facebook-android-sdk facebook-sdk-4.0

140
推荐指数
7
解决办法
15万
查看次数

设置宽度以匹配ConstraintLayout中的约束

我想将View的左侧和右侧约束到它的父视图的边距,并使其填充分配的空间.但是,将宽度设置为match_parent或者wrap_content似乎产生相同的结果.

有没有相当于match_constraints的东西(而不是match_parent和wrap_content)?难道match_parentwrap_content影响布局还是他们在新的约束布局忽略?

爱我最喜欢的平台的新布局系统!

android android-constraintlayout

79
推荐指数
6
解决办法
7万
查看次数

如何阻止logcat在Android Studio中滚动

我之前使用过Eclipse,我可以很容易地阻止logcat滚动,但我在Android工作室找不到同样的功能.谁知道怎么做?

android android-logcat android-studio

73
推荐指数
3
解决办法
2万
查看次数

如何在设计支持库的TabLayout中的Tabs之间设置分隔符?

我正在使用新android.support.design.widget.TabLayoutv7-appcompat库,并发现了一个问题,没有办法在选项卡之间设置分隔符,不知道是否有.

我已经成功配置了寻呼机适配器,并且标签看起来不错,但无法在标签之间设置分隔线.

我想要这种类型的标签

Tab1 | Tab2 | Tab3
Run Code Online (Sandbox Code Playgroud)

但目前正在展示

Tab1  Tab2  Tab3
Run Code Online (Sandbox Code Playgroud)

我的xml是

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" >

        <include layout="@layout/toolbar" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tablayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/shape_tabbar_background"
            app:tabIndicatorColor="@android:color/white"
            app:tabIndicatorHeight="4dp" />
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

我正在添加标签

viewPager = (ViewPager) findViewById(R.id.viewpager);
    viewPager.setOffscreenPageLimit(2);
    adapter = new TabAdapterLoginActivity(getSupportFragmentManager(),
            titles);
    viewPager.setAdapter(adapter);
    tabLayout = (TabLayout) findViewById(R.id.tablayout);
    tabLayout.setupWithViewPager(viewPager);
Run Code Online (Sandbox Code Playgroud)

android androiddesignsupport

60
推荐指数
2
解决办法
4万
查看次数

如何在android中更改位图图像颜色?

我正在开发一个Android应用程序,在其中我将图像设置为imageview.现在编程我想改变位图图像颜色.假设我的图像最初是红色的,现在我需要将其更改为橙色.我怎样才能做到这一点?请帮忙.

这是我的代码.我设法改变了不透明度,但我不知道如何改变颜色.

  @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ImageView iv = (ImageView) findViewById(R.id.img);
        Drawable d = getResources().getDrawable(R.drawable.pic1);
        Bitmap mNewBitmap = ((BitmapDrawable)d).getBitmap();
        Bitmap nNewBitmap = adjustOpacity(mNewBitmap);
        iv.setImageBitmap(nNewBitmap);
    }

    private Bitmap adjustOpacity( Bitmap bitmap ) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        Bitmap dest = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        int[] pixels = new int[width * height];
        bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
        dest.setPixels(pixels, 0, width, 0, 0, width, height);
        return dest;
    } 
Run Code Online (Sandbox Code Playgroud)

android bitmap

43
推荐指数
6
解决办法
7万
查看次数

我们什么时候使用recyclerView.setHasFixedSize?

事情是这样的:有人知道setHasFixedSize方法吗?有人说如果所有项目大小都相同,它允许进行优化,而在android.support.v7.widget的RecyclerView类中,它评论如下:如果事先可以知道适配器内容的变化,RecyclerView可以执行多次优化无法改变RecyclerView本身的大小.如果您使用RecyclerView属于此类别,请将此设置为true.

那是什么意思?有人能告诉我使用它的背景或解释上面"这个类别"的含义吗?非常感谢.

android

28
推荐指数
1
解决办法
2万
查看次数

即使插入耳机,如何通过扬声器播放音频?

问题的标题可能看起来重复,但我的问题有时是问题并导致故障.插入耳机时,我使用以下代码播放扬声器.

AudioManager audioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);

        audioManager.setMode(AudioManager.STREAM_MUSIC);
        audioManager.setSpeakerphoneOn(true);

        if(! Globals.mediaPlayer.isPlaying()){
            Globals.mediaPlayer.start();
        }
Run Code Online (Sandbox Code Playgroud)

上面的代码通过以下方式播放音频:1.很少次,它播放完美.2.大多数时候,它在背景中播放循环声.几次,它什么都不玩.

插入耳机时似乎系统声音播放没有错误.例如 - 设置铃声正确播放相应的铃声,没有任何故障.请帮我理解如何通过插入耳机的扬声器正确播放声音.

android android-audiomanager

21
推荐指数
2
解决办法
1万
查看次数

使用新的Facebook Android SDK访问用户电子邮件

嗨,我正在玩新的Facebook Android SDK.我无法弄清楚的一件事是我如何要求电子邮件地址.我尝试了以下但它返回了除电子邮件地址以外的所有字段的值.我想我可能需要单独询问电子邮件地址的权限,但如果我使用LoginFragment,我不知道如何做到这一点.

Request request = Request.newMeRequest(session, new GraphUserCallback() {

    @Override
    public void onCompleted(GraphUser user, Response response) {
        profilePictureView.setUserId(user.getId());
        userNameView.setText(user.getName());
    }
});

String NAME = "name";
String ID = "id";
String PICTURE = "picture";
String EMAIL = "email";
String FIELDS = "fields";
String REQUEST_FIELDS = TextUtils.join(",", new String[] {
    ID, NAME, PICTURE, EMAIL
});

Bundle parameters = new Bundle();
parameters.putString(FIELDS, REQUEST_FIELDS);
request.setParameters(parameters);
Request.executeBatchAsync(request);
Run Code Online (Sandbox Code Playgroud)

任何帮助表示赞赏.谢谢

android facebook-android-sdk

19
推荐指数
3
解决办法
3万
查看次数

如何创建类似于Play报亭布局的内容

我正试图"重新创建"(类似于)Play报亭的布局.根据文档,CoordinatorLayout的一般结构是

<CoordinatorLayout>
    <!—  view that shrinks —>
    <AppBarLayout>
        <CollapsingToolbarLayout>
            …
        </CollapsingToolbarLayout>
    </AppBarLayout>

    <!— view that scrolls —>
    <SomeScrollView >
        ….
    </SomeScrollView>
</CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

除了我看报亭应用程序外,我能看到的是它SomeScrollView实际上是一个用于TabLayout的ViewPager.对于我的特殊情况,我的主要问题是我SomeScrollView必须成为碎片的某种容器.基本上我想要的是

-------
|   A |
|     |
-------
|  B  |
|     |
-------
Run Code Online (Sandbox Code Playgroud)

其中A是可折叠部分,B是滚动部分.同样,对于我的情况,B是动态片段的容器.所以A将有一个TabLayout,当用户点击一个标签时,它会导致B中的可见片段发生变化.B中的片段将包含RecycleView或可滚动的TextView.(实际上其中一个片段是一个FrameLayout,它包含一个RecycleView和一个TextView,其中任何一个都是可见的)

到目前为止,这是我的代码:

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <!-- collapsing view -->
    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <include
                …
                />

            <android.support.design.widget.TabLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_scrollFlags=“…”
                />

        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>

    <!-- scrolling view -->
    <android.support.v4.view.ViewPager
        android:id="@+id/main_viewpager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        > …
Run Code Online (Sandbox Code Playgroud)

android android-layout android-coordinatorlayout

19
推荐指数
1
解决办法
1980
查看次数