小编Raw*_*awa的帖子

升级到Android 8.1后,startForeground失败

将手机升级到8.1开发人员预览后,我的后台服务不再正常启动.

在我长期运行的服务中,我实现了一个startForeground方法来启动正在进行的通知,该通知在create上调用.

    @TargetApi(Build.VERSION_CODES.O)
private fun startForeground() {
    // Safe call, handled by compat lib.
    val notificationBuilder = NotificationCompat.Builder(this, DEFAULT_CHANNEL_ID)

    val notification = notificationBuilder.setOngoing(true)
            .setSmallIcon(R.drawable.ic_launcher_foreground)
            .build()
    startForeground(101, notification)
}
Run Code Online (Sandbox Code Playgroud)

错误信息:

11-28 11:47:53.349 24704-24704/$PACKAGE_NAMEE/AndroidRuntime: FATAL EXCEPTION: main
    Process: $PACKAGE_NAME, PID: 24704
    android.app.RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=My channel pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x42 color=0x00000000 vis=PRIVATE)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1768)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Run Code Online (Sandbox Code Playgroud)

服务通知的通道无效,显然我的旧通道DEFAULT_CHANNEL_ID不再适用于我假设的API 27.什么是合适的渠道?我试图浏览一下文档

service android background-service android-notifications kotlin

159
推荐指数
6
解决办法
8万
查看次数

奇怪的赋值,TextView到Bundle,反编译后,为什么?

我创建了一个简单的应用程序,一个计数器应用程序,按下按钮会增加一个整数并更新文本视图.代码如下:

public class MainActivity extends Activity {
    public static int count = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final TextView textView = (TextView) findViewById(R.id.count);
        textView.setText(Integer.toString(count));
        final Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                count++;
                textView.setText(Integer.toString(count));
            }
        });
    }
    ...
}
Run Code Online (Sandbox Code Playgroud)

在使用dex2jar和jd-gui反编译同一个应用程序后,我收到了以下代码:

public class MainActivity extends Activity {
    public static int count = 0;

    protected void onCreate(final Bundle paramBundle) {
        super.onCreate(paramBundle);
        setContentView(2130903040);
        paramBundle = (TextView)findViewById(2131296257);
        paramBundle.setText(Integer.toString(count));
        ((Button)findViewById(2131296256)).setOnClickListener(new View.OnClickListener() {
            public void …
Run Code Online (Sandbox Code Playgroud)

java android decompiling

17
推荐指数
1
解决办法
906
查看次数

Kotlin 覆盖抽象 val 行为、对象与类

我刚刚开始使用并开始弄乱抽象类,覆盖 val 和 singelton。但是,我刚刚遇到了一个非常奇怪的行为。我的目标是拥有一个抽象类,然后创建几个扩展该抽象类的单例。因为我想需要某些变量,所以我创建了抽象 val,然后可以在子类中覆盖它(而不是通过构造函数传递它们)。

所以我有 4 节课:

主要活动:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val instance = Instance()
        Log.d("MainActivity", "instance randObject: ${instance.randObject}")
        Log.d("MainActivity", "instance randObject: ${instance.randObject.myProp}")
        Log.d("MainActivity", "instance randObject: ${instance.randObject.myProp2}")

        Log.d("MainActivity", "singleton randObject: ${Object.randObject}")
        Log.d("MainActivity", "singleton randObject: ${Object.randObject.myProp}")
        Log.d("MainActivity", "singleton randObject: ${Object.randObject.myProp2}")    
    }
}
Run Code Online (Sandbox Code Playgroud)

实例:

class Instance: AClass(){
    override val testString: String = "test"
    override val testUriString: String = "https://www.google.se"
    override val testUri: Uri = Uri.parse(testUriString)!!
    override val randObject: RandomObject = RandomObject("Herp")
} …
Run Code Online (Sandbox Code Playgroud)

singleton android object abstract kotlin

6
推荐指数
1
解决办法
5672
查看次数

Android Wear通知显示最小化

我不确定我做错了什么,通知是我喜欢它的方式,但我不能让它让手表振动,它显示为最小化.我想要实现的效果应该看起来像环聊通知,它会振动并全屏显示.这是我正在使用的代码(在手表上):

Intent actionIntent = new Intent(this, ConvActivity.class);
            actionIntent.putExtra("num",num);
            PendingIntent actionPendingIntent =
                    PendingIntent.getActivity(this, 0, actionIntent,
                            PendingIntent.FLAG_UPDATE_CURRENT);

// Create the action
                NotificationCompat.Action action =
                        new NotificationCompat.Action.Builder(R.drawable.ic_launcher,"Reply"
                                , actionPendingIntent)
                                .build();
            NotificationCompat.BigTextStyle bigStyle = new NotificationCompat.BigTextStyle();
            bigStyle.bigText(body).setBigContentTitle(name);


            NotificationCompat.Builder notificationBuilder =
                    new NotificationCompat.Builder(this)
                            .setSmallIcon(R.drawable.ic_launcher)
                            .setContentTitle(name)
                            .setContentText(body)
                                    //.setContentIntent(viewPendingIntent)
                                    //.addAction(R.drawable.ic_map,
                                    //        getString(R.string.map), mapPendingIntent)
                            .setStyle(bigStyle).setAutoCancel(true)
                    .addAction(R.drawable.ic_launcher,"Reply"
                            , actionPendingIntent);

            if(pic != null)
                notificationBuilder.setLargeIcon(BitmapFactory.decodeByteArray(pic,0,pic.length));
                            //.extend(new NotificationCompat.WearableExtender().addAction(action))                ;




            // Get an instance of the NotificationManager service
            NotificationManagerCompat notificationManager =
                    NotificationManagerCompat.from(this);

// Issue the notification with notification manager.
                notificationManager.notify(0, notificationBuilder.build());
Run Code Online (Sandbox Code Playgroud)

android wear-os

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