gpt4 book ai didi

java - 如何在android的拨号器(默认或股票拨号器)中启动服务

转载 作者:太空宇宙 更新时间:2023-11-04 12:23:01 26 4
gpt4 key购买 nike

我想在我的拨号器(默认或默认拨号器)打开时启动服务。我的服务只是 toast 消息。如何启动拨号器服务。 startService 命令在 MainActivity 中工作,但在拨号器打开时不起作用。

我的代码如下所示:

Manifest.xml

 <?xml version="1.0" encoding="utf-8"?>    

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.chatheads">

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.DIAL_ACTION" />
<uses-permission android:name="android.permission.CALL_PHONE" />


<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<service
android:name=".MyService"
android:enabled="true"
android:exported="true" />

<activity android:name=".SecondActivity"></activity>
</application>

</manifest>

MainActivity.java

package com.example.chatheads;

import android.support.v7.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.text.method.DialerKeyListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class MainActivity extends AppCompatActivity {
Button startService,stopService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startService=(Button)findViewById(R.id.startService);
stopService=(Button)findViewById(R.id.stopService);




if(getIntent().getAction().equals(Intent.ACTION_DIAL)) {
Intent intent = new Intent(getBaseContext(), MyService.class);
startService(intent);

}

startService.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
startService(new Intent(getApplication(), MyService.class));




}
});
stopService.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
stopService(new Intent(getApplication(), MyService.class));



}
});


}
}

Myservice.java

package com..service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

public class MyService extends Service {
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Toast.makeText(this, "Service Created", Toast.LENGTH_LONG).show();

}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
intent.getStringExtra("MY_MESSAGE");
//stopSelf();
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Toast.makeText(this, "Service Stopped", Toast.LENGTH_LONG).show();

}

@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}

}

此代码不起作用。当MainActivity中调用startService时服务启动。但是当拨号器打开时它不起作用。

最佳答案

我想也许你可以尝试将 getApplication () 更改为 MainActivity.this

而且我认为您需要从拨号器跳转到 MainActivity 才能启动服务

关于java - 如何在android的拨号器(默认或股票拨号器)中启动服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38634027/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com