我有一个应用程序“x”按钮单击启动服务(在某些其他程序包中)。此服务现在应该读取通话记录并将数据发送到应该将数据写入SD卡的应用程序“y”。目前,我的服务运行在点击按钮上,但应用程序“y”并不像我在应用程序“y”中拥有一杯祝酒,而且它永远不会出现。读取日志时会给出权限,并且因为使用自定义操作,所以应用程序“y”的意图过滤器中也包含相同的权限。有人能说出这个问题的原因吗?服务无法启动其他应用程序的活动
服务代码
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("TAG", "Service started.");
super.onStartCommand(intent, flags, startId);
StringBuffer sb = new StringBuffer();
String strOrder = android.provider.CallLog.Calls.DATE + " DESC";
/* Query the CallLog Content Provider */
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CALL_LOG) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return 0;
}
Cursor managedCursor = getContentResolver().query(CallLog.Calls.CONTENT_URI, null,
null, null, strOrder);
int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
sb.append("Call Log :");
int h = 0;
while (managedCursor.moveToNext() && h != 20) {
String phNum = managedCursor.getString(number);
String callTypeCode = managedCursor.getString(type);
String strcallDate = managedCursor.getString(date);
Date callDate = new Date(Long.valueOf(strcallDate));
String callDuration = managedCursor.getString(duration);
String callType = null;
h++;
int callcode = Integer.parseInt(callTypeCode);
switch (callcode) {
case CallLog.Calls.OUTGOING_TYPE:
callType = "Outgoing";
break;
case CallLog.Calls.INCOMING_TYPE:
callType = "Incoming";
break;
case CallLog.Calls.MISSED_TYPE:
callType = "Missed";
break;
}
sb.append("\nPhone Number:--- " + phNum + " \nCall Type:--- "
+ callType + " \nCall Date:--- " + callDate
+ " \nCall duration in sec :--- " + callDuration);
sb.append("\n----------------------------------");
}
managedCursor.close();
managedCursor.close();
Intent sendIntent = new Intent();
String CUSTOM_ACTION = "com.example.sonali.callreadingservice.CALL_LOGS";
sendIntent.setAction(CUSTOM_ACTION);
sendIntent.putExtra(Intent.EXTRA_TEXT, (Serializable) sb);
sendIntent.setType("text/plain");
startActivity(sendIntent);
this.stopSelf();
return 0;
}
@Override
public void onDestroy() {
Log.d("slog", "onDestroy()");
super.onDestroy();
}
应用 “y” 的主要活动
``public class MainActivity extends AppCompatActivity {
TextView textView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show();
textView = (TextView) findViewById(R.id.textview_call);
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
handleSendText(intent);
}
void handleSendText(Intent intent) {
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
try {
if (sharedText != null) {
textView.setText(sharedText);
File sdCard = Environment.getExternalStorageDirectory();
FileWriter writer = new FileWriter(sdCard);
// Writes the content to the file
writer.write(sharedText);
writer.flush();
writer.close();
}
} catch (Exception e) {
}
}
应用的意图过滤器 “Y” `
`<intent-filter>
<action android:name="com.example.sonali.callreadingservice.CALL_LOGS" />
<category android:name="android.intent.category.LAUNCHER" />
<data android:mimeType="text/plain" />
</intent-filter>
您是否定义了要从服务清单内的应用程序Y运行的活动? –
你需要什么服务?只是发送'Cursor'的数据?为什么不使用“ContentProvider”? – pskink
@MuhammedRefaat不,我没有。你能告诉我怎么写吗? –